Codeigniter - checkbox form validation

14,895

Don't use isset() in CodeIgniter as CodeIgniter provide better class to check if the POST Variable you are checking is exist or not for example try to use this code instead of your code:

if($this->input->post('groupcheck')):
   $this->form_validation->set_rules('groupcheck[]', 'groupcheck', 'required');
endif;

For Guidline using on how to use POST and GET variables in CodeIgniter check the User Guide here: http://codeigniter.com/user_guide/libraries/input.html

Share:
14,895
Ben
Author by

Ben

Updated on June 04, 2022

Comments

  • Ben
    Ben almost 2 years

    I have a form validation rule in place for a form that has a number of checkboxes:

    $this->form_validation->set_rules('groupcheck[]', 'groupcheck', 'required');
    

    If none of my checkboxes are checked upon submission, my code never gets past the validation->run as the variable does not exist:

    if ($this->form_validation->run()):
    

    If i surround my validation rule with a check for the var, the validation never passes as there are no other form validation rules:

    if(isset($_POST['groupcheck'])):
       $this->form_validation->set_rules('groupcheck[]', 'groupcheck', 'required');
    endif;
    

    How can I manage a checkbox validation rule where the var may not exist, and it will be the only form variable?

    Regards, Ben.