Welcome Guest, Not a member yet? Register   Sign In
Weird problem with form validation
#1

[eluser]Unknown[/eluser]
I have a form with checkbox inputs that visitor should select atleast one of checkboxes ...
after few hours searching and testing I found a weird issue !

Controller:
Code:
$this->form_validation->set_rules('selected_checkboxes', 'Checkboxes', 'required|isset');
        if ($this->form_validation->run() != FALSE)
        {
            //one of checkboxes selected .. countiune processing...
        }
        $this->load->view('form');

View 1 :
Code:
<?php echo validation_errors(); ?>
<form method=post>
    &lt;input type="checkbox" name="selected_checkboxes[]" value="1" /&gt;A  <br>
    &lt;input type="checkbox" name="selected_checkboxes[]" value="2" /&gt;B  <br>
    &lt;input type="submit" value="submit"&gt;
&lt;/form&gt;

View 2 :
Code:
&lt;?php echo validation_errors(); ?&gt;
&lt;form method=post&gt;

    &lt;input type="hidden" name="jsfsdf" value="dfdfsdg"&gt; // <<<<<<<<=============

    &lt;input type="checkbox" name="selected_checkboxes[]" value="1" /&gt;A  <br>
    &lt;input type="checkbox" name="selected_checkboxes[]" value="2" /&gt;B  <br>
    &lt;input type="submit" value="submit"&gt;
&lt;/form&gt;

in the first view when I am submitting the form without selecting any checkbox its not showing any error ! but when selecting one of checkboxes its working fine ...

but in the second view when I added a input in the form ( it doesn't matter what the input type is ! text or hidden or ... ), form validating is working fine !
mean when I don't select any of checkboxes its showing validating error !

I don't know what miracle is doing that extra input in the form ...

Thanks
#2

[eluser]tomcode[/eluser]
In the first example the $_POST array rests empty, thus the form validation library does not run through. You have to have at least one entry in the $_POST array to trigger the validation, which is the case in Your second example.




Theme © iAndrew 2016 - Forum software by © MyBB