Search code examples
codeignitercodeigniter-form-helper

form _dropdown validation for codeigniter using callback


I need to know how the validation for drop down works in codeigniter what need to be written in view, controller ,model .

Below is my code but its not working .

view.php

 $options = array(
              '0'    => 'Select',
              '1'  => 'Dr.',
              '2'  => 'Mr.',
              '3' => 'Mrs.',
              '4' => 'Ms.',
              '5' => 'Prof.',
              '6' => 'Mr. & Mrs.',
            );

<?php echo form_dropdown('Title', $options, set_value('title')); ?>

controller.php

    $this->form_validation->set_rules('title','Titles','required|callback_select_validate');

function select_validate() { 
 $gender = $this->input->post('gender'); 
 if($gender == 0) {
    return true;
  } 
 else { 
  $this->form_validation->set_message('check_sex', 'invalid choice');
 return FALSE; 
} 

Solution

  • Now it worked

    view.php

     $options = array(
                  '0'    => 'Titles',
                  'Dr.'  => 'Dr.',
                  'Mr.'  => 'Mr.',
                  'Mrs.' => 'Mrs.',
                  'Ms.' => 'Ms.',
                  'Prof.' => 'Prof.',
                  'Mr. & Mrs.' => 'Mr. & Mrs.',
                );
    <?php echo form_dropdown('Title', $options, set_value('Title')); ?>
    

    controller.php

    $this->form_validation->set_rules('Title','Titles','required|callback_select_validate');
    
    
    $this->form_validation->set_message('select_validate', 'This %s is a default value');
    
    
    
    
    
     function select_validate($post_string) {
                        return $post_string == '0' ? FALSE : TRUE;
                     }