CodeIgniter Forums
validation on a checkbox - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: validation on a checkbox (/thread-6776.html)

validation on a checkbox - El Forum - 03-11-2008

when validating a checkbox with name='check', what rule should i use to make it gets checked? 'required' doesnt seem to work and the form passes validation even when its not checked.


validation on a checkbox - El Forum - 03-23-2008

I was searching forums about this today. There are a lot of good extensions to validation class, but if all you need is to make sure that checkbox (or at least one checkbox in array) is checked, you have to add two rules:

$rules['chkArray'] = 'required|isset';

"isset" rule is undocumented, not sure why.

I hope someone will find this useful.

validation on a checkbox - El Forum - 04-17-2008

[quote author="steelaz" date="1206316814"]"isset" rule is undocumented, not sure why.[/quote]
Hi - actually it's not undocumented. The validation docs states that *any* PHP function that accepts a single parameter can be used in validation rules, i.e. "is_numeric", etc...

validation on a checkbox - El Forum - 04-17-2008

That true, but validation class is treating "isset" separately. This comment is from Validation.php :
* Are we dealing with an "isset" rule?
* Before going further, we'll see if one of the rules
* is to check whether the item is set (typically this
* applies only to checkboxes).  If so, we'll
* test for it here since there's not reason to go
* further

validation on a checkbox - El Forum - 05-29-2008

@steelaz, thank you, I found this very useful indeed! After spending some hours trying to make validation of a checkbox work and setting a custom error message for the checkbox I found this post and it finally works now! Here's my code (snippet)

// Set Rules for required fields
    $rules['sender_name'] = "required";
    $rules['from'] = "required|valid_email";
    $rules['to'] = "required|valid_email";
    $rules['terms'] = "required|isset";


// Set multilingual custom error messages
    $this->validation->set_message('required', $this->lang->line('Required'));
    $this->validation->set_message('valid_email', $this->lang->line('ValidEmail'));
    $this->validation->set_message('isset', $this->lang->line('PleaseAcceptTerms'));