Welcome Guest, Not a member yet? Register   Sign In
Basic checkbox validation not working - grad students + please help!!!
#1

[eluser]CIfan1000[/eluser]
I have the following controller:
Code:
<?php

class Checkboxtest extends Controller {

    function index()
    {

        // Load CI helpers required for form validation:
        $this->load->helper(array('form', 'url'));

        // Load the validation library:
        $this->load->library('validation');
        
        // Define the validation rules for each field:
        // The names must be the field names in the form:
        $rules['CheckboxUserAgreement']     = "isset";
        
        $this->validation->set_rules($rules);
        
        // Repopulate the form field with the submitted data
        // The array keys are the actual names of the form fields,
        // the value represents the full name that you want shown in the error message.
        $fields['CheckboxUserAgreement'] = 'User agreement checkbox';

        $this->validation->set_fields($fields);
        
        $this->validation->set_message('isset', 'Please Accept Terms');

        if ($this->validation->run() == FALSE)
        {
            // If validation is false then (re)display the form:
            
            $data['title'] = "Registration";
            $data['bodyview'] = "register/checkbox";
            $this->load->view('templates/template01',$data);
            
        }
        else
        {

                    $data['title'] = "Registration successful";
                    $data['bodyview'] = "register/registersuccess";
                    $this->load->view('templates/template01',$data);
        }

    }

}
?>

With the following view:

Code:
<h1>Checkbox test</h1>

&lt;?php

    // Version record
        // register01.01.01
            // Added checkbox

    // This line is required by the controller to display
    // validation errors in the form:
    echo $this->validation->error_string;

    // Create a form using CI
    // Validation and reshowing this form is handled by the controller
    // The argument for the form_open function must be the name of the Controller:
    echo form_open('checkboxtest');
    
?&gt;


    &lt;input type="checkbox" name="CheckboxUserAgreement" value="1" &lt;?php echo $this-&gt;validation->set_checkbox('CheckboxUserAgreement', '1'); ?&gt; />

    I have read and agree to the User Agreement<br/><br/>

    <div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

    &lt;/form&gt;

And I do not get a validation message if I do not check the checkbox and press Submit.

I did at one time when I had the validation rule originally set to "required" and it worked.

The problem with using "required" originally was that the validation message was "

The User agreement checkbox field must have a value.

I would like it to say: The User agreement checkbox field is required.

So then I changed it to "isset". That didnt work (no validation message).

Then I changed it back to "required" and now this also doesn't provide a validation message.

Maybe a bug?

I have spent 6 hours today trying different things including writing the above code to isolate it only to a checkbox.

I have also tried a callback function that also doesnt work.

I am starting to tear my limited amount of hair out!!!

Any help I would be very grateful for!!!!!
#2

[eluser]Pascal Kriete[/eluser]
Can't spot anything wrong at first glance.
Could it be the language file? Is the file loaded?
What happens if you use a string there instead.
[/code]
$this->validation->set_message('isset', 'You must accept the terms');
[/code]
#3

[eluser]CIfan1000[/eluser]
Hi inparo,

Thanks for the suggestion.

I made the change in my controller and unfortunatley it did not make a difference.

I edited my post above to reflect the change in the controller.

Please let me know if you have any other suggestions.
#4

[eluser]Pascal Kriete[/eluser]
Ah, I'm silly. Validation will only run if the $_POST array has at least one value.
So as is it won't work if the checkbox isn't set, since that means that there is no POST data. Try naming your submit button, or add another field to the form.
#5

[eluser]CIfan1000[/eluser]
Yes! That worked! Thank you, thank you!

However, it didnt work quite the way I expected.

I added the name to the submit button:
Code:
<div>&lt;input type="submit" name="SubmitButton" value="Submit" /&gt;&lt;/div>
But then if I just used isset for the rule, then even if the checkbox was unchecked, validation was (incorrectly) true and the registersuccess view was shown.

I am not sure why this would happen, but I changed the rule to "required", and then the validation worked properly, but the validation message for the "isset" was shown.
Code:
$this->validation->set_message('isset','Please read and agree to the User Agreement.');

Again, I am not sure why this would happen....

But I read that one should use "required|isset":
Code:
$rules['CheckboxUserAgreement']     = "required|isset";

I tried this and now I still get the validation message for the "isset".

But I am happy with this.

I very much appreciate you helping me out on this!
#6

[eluser]hvalente13[/eluser]
Some changes are commited in the svn version of codeigniter.

You may want to check it out and see if the Validation Class permits you to do what you want. I've not tested it but it's worth to try it.

SVN Repository
#7

[eluser]CIfan1000[/eluser]
Thanks hvalente13 - I will try that.




Theme © iAndrew 2016 - Forum software by © MyBB