2

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.

4
  • What is required for ? If you need at least one checkbox checked then your first code is ok. If it is not required, just remove it... Commented Aug 10, 2011 at 18:27
  • @ldiqual - Thanks for the pointer. I removed the "required" but it still fails to run when no check boxes are selected: code $this->form_validation->set_rules('groupcheck[]', 'groupcheck', ''); if ($this->form_validation->run()): echo "test"; Commented Aug 10, 2011 at 18:39
  • The only option I can think of is to put a hidden form field in, and validate against that. That way a validation rule will always pass. Seems like a hack though. Commented Aug 10, 2011 at 18:50
  • Your if statement needs to have a true or false like this "if ($this->form_validation->run() == TRUE) " might not solve your problem but is a good practice Commented Aug 10, 2011 at 23:23

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the pointer, but im not sure how that wil allow my form validation to run if the var is not set. It will still fail at: code if ($this->form_validation->run()): code
Its failing because you set the groupcheck to required and that makes the groupcheck checkboxes to be checked atleast one should be checked because its required.
0

I had the same issue. If your checkbox is unchecked then it will never get posted. Remove the set_rules for your checkboxes and after your other form validation rules, try something like:

    if ($this->form_validation->run() == TRUE){ // form validation passes 

        $my_checkbox_ticked = ($this->input->post('my_checkbox')) ? yes : no;

Comments

0

You may compare validation_errors() after $this->form_validation->run() if is FALSE then nothing was validate, so you can do something or show a warning

if ($this->form_validation->run() == FALSE) {
    if (validation_errors()) {
        echo validation_errors();
    } else {
        echo 'empty';
    }
}

Comments

0

You also need to set button submit

    $this->form_validation->set_rules('terminos_form', 'TERM', 'required');
    $this->form_validation->set_rules('terminosbox', 'TERM BOX', 'callback__acept_term');

Callback

    function _acept_term($str){
        if ($str === '1'){ 
            return TRUE;
        }
            $this->form_validation->set_message('_acept_term', 'Agree to the terms');
            return FALSE;
}

HTML

<input type="checkbox" name="terminosbox" value="1"/>
<button type="submit" name="terminos_form" value="1">NEXT</buttom>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.