zen of coding

Validating a checkbox in CakePHP 1.2

In a few simple words: use the ‘comparison’ rule to validate a checkbox.

To give you an example, let’s say a user needs to agree to the terms of service when registering a new account. In your User model you setup a rule for the checkbox as follows:

 'agree' => array(
              'rule' => array('comparison', '!=', 0),
              'required' => true,
              'message' => 'You must agree to the terms of use',
                'on' => 'create'
       )

As you can probably guess from the code the empty checkbox will send a default value of zero, and that is what your validation rule is going catch. The ‘on’ key will ensure that the rule is only enforced on account creation and not when the User is editing an existing account.

  • thanks, I was just looking for that

  • teknoid

    @yaqoob

    Cool

  • clouseau

    Thanks! This really helped!

  • @clouseau

    No problem.

  • sandeep

    Many Thanks, That RRReally helped :-)

  • @sandeep

    Good to hear ;)

  • Pingback: Validating a checkbox in CakePHP 1.2 « The Technological African's Blog()

  • Pingback: Validating a checkbox in CakePHP 1.2 « The Technological African's Blog()

  • Pingback: Validating a checkbox in CakePHP 1.2 « The Technological African's Blog()

  • Pingback: Validating a checkbox in CakePHP 1.2 « The Technological African's Blog()

  • Hello, is there any way to make checkbox unchecked using $form->input(…, array(‘type’=>’checkbox’)); ?

  • @sinx

    Setting the ‘checked’ to FALSE possibly.
    Otherwise, please check the API.

  • You rock!!!

  • @Tim Gurske

    Thank you ;)

  • Great! I was just looking for that!

  • Thijs

    First of all, thank you! Your website has been very useful in the process of learning and working with CakePHP.

    I do have a question about checkbox validation in cakephp >1.3<. Could it be that something has changed and made it stop working correctly? Validating with the code you provide above seems to work. When the 'agree' checkbox is not checked an error message is shown which is good. But when it is checked no error is shown and I'm being returned to my form. The error seems to be in the checkbox-validation part because leaving this part out and then submitting and testing the form works like it should. Any suggestions? Thank you in advance!

  • @ Thijs

    Check $this->validationErrors(); to see which ones are actually triggered.
    Not sure of any changes in 1.3 regarding this.

    You can also review test cases for the validation rules.

  • Thijs

    @teknoid
    Thank you, you’re the best!
    pr($this->validationErrors()); in my view did the trick. Apparently a hidden field that I was ‘sending along’ didn’t validate, but ofcourse that error wasn’t shown or printed, not ‘on screen’ and not in the page source either..

    Everything fixed now, thanks again! Keep up posting! :)

  • @ Thijs

    Good to hear. Thanks ;)

  • That is looking for, thanks!

  • arcana

    Hello,

    Just wanted to say thanks for your blog and this post in particular. Exactly what I was looking for, especially the ‘on’ => ‘create’ bit, I’m sure that’ll save me a bit of a headache.

    Cheers,
    -arcana

  • Hi, i have a little problem too with a checkbox.
    I validate the formfields in a model with the ‘comparison’.
    i want cake to output an error-message below the checkbox if the checkbox is not checked. But cake ignores all what i do.
    checkbox called so: echo $this->Form->checkbox(‘Info’, array(‘label’ => false));
    in the model:
    var $validate = array(‘Info’ => array(‘rule’ => array(‘comparison’, ‘!=’, 0), ‘message’ => ‘Hit Checkbox’)));
    All the other fields i validated works great ane error message will displayed. but only the checkbox makes trouble. I hope anybody can solve my problem.

    greetings from germany Maik

  • Thanks a lot for your help!

  • Just Perfect !

%d bloggers like this: