php - my form validation is not working properly for select box in codeigniter -


i working on form validation in codeigniter create validation rules form in controller. have 2 select box , want validate both problem both select box show same error message want show different message each select box.

the main problem error message show same error of city both select box.. want different error message different select box.

and 1 more question :: when validation error message 1 field whole form got empty. want show when got validation error field other field data there.

this controller:

public function addemployeecontroller() {      $this->load->library('form_validation');      $abcd = $this->input->post('city_id');     $abc = $this->input->post('desi_id');       $this->form_validation->set_rules('emp_name', 'name', 'trim|required');     $this->form_validation->set_rules('emp_jdate', 'joining date', 'trim|required');     $this->form_validation->set_rules('emp_addr', 'address', 'trim|required');     $this->form_validation->set_rules('emp_sal', 'salary', 'trim|required');     $this->form_validation->set_rules('emp_descr', 'description', 'trim|required');     $this->form_validation->set_rules('emp_mobile', 'mobile number', 'trim|required|min_length[10]');     $this->form_validation->set_rules('city_id', 'city', 'trim|required|callback_select_validate');     $this->form_validation->set_rules('desi_id', 'designation', 'trim|required|callback_select_validate');        if($this->form_validation->run() == false)     {         $this->index();              }      else     {                      if($query = $this->emp_model->addemployeemodel('$data'))         {             $data['main_content'] = 'signup_successful';             $this->load->view('emp_view', $data);         }         else         {             $this->load->view('emp_view');                   }     }  }  public function select_validate($abcd) {         // 'none' first option default "-------choose city-------"         if($abcd == "none")         {                 $this->form_validation->set_message('select_validate', 'please select city.');                  return false;         }          else             {                 // user picked something.                 return true;             } }  public function select_validate1($abc) {         // 'none' first option default "-------choose city-------"         if($abc = "none")         {                 $this->form_validation->set_message('select_validate', 'please select designation.');                 return false;         }          else             {                 // user picked something.                 return true;             } } 

and in view have select box this. when submit form without selecting select box show error message of "please select city" both select box. want show different message.

<p>     <lable for="desi_id">designation:</lable><?php echo form_error('desi_id'); ?>          <select name="desi_id">         <option selected="selected" value="none">select post</option>             <?php foreach ($records $row) { ?>          <option value="<?php echo $row->desi_id ?>"><?php echo $row->post_name ?></option>         <?php } ?>           </select>      </p> 

city: select city city_id?>">city_name?>

hello different message need define:

for message :

    $this->form_validation->set_rules('emp_name', 'name', 'trim|required');     $this->form_validation->set_rules('emp_jdate', 'joining date', 'trim|required');     $this->form_validation->set_rules('emp_addr', 'address', 'trim|required');     $this->form_validation->set_rules('emp_sal', 'salary', 'trim|required');     $this->form_validation->set_rules('emp_descr', 'description', 'trim|required');     $this->form_validation->set_rules('emp_mobile', 'mobile number', 'trim|required|min_length[10]');     $this->form_validation->set_rules('city_id', 'city', 'trim|required|callback_select_validate');     $this->form_validation->set_rules('desi_id', 'designation', 'trim|required|callback_select_validate1');      $this->form_validation->set_message('city_id', 'your message');     $this->form_validation->set_message('desi_id', 'your other message'); 

for empty other fields : have use set_value('field_name') in fileds.

<input type="text" name="city" value="<?php echo set_value('city'); ?>" /> 

your minor mistake : callback_select_validate and callback_select_validate1

$this->form_validation->set_rules('city_id', 'city', 'trim|required|callback_select_validate'); $this->form_validation->set_rules('desi_id', 'designation', 'trim|required|callback_select_validate1'); 

hope :)


Comments