Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate a checkbox in ZF2

I've read numerous workarounds for Zend Framework's lack of default checkbox validation.

I have recently started using ZF2 and the documentation is a bit lacking out there.

Can someone please demonstrate how I can validate a checkbox to ensure it was ticked, using the Zend Form and Validation mechanism? I'm using the array configuration for my Forms (using the default set-up found in the example app on the ZF website).

like image 622
Alex Avatar asked Nov 19 '12 18:11

Alex


3 Answers

Try this

Form element :

$this->add(array(
            'type' => 'Zend\Form\Element\Checkbox',
            'name' => 'agreeterms',
            'options' => array(
                'label' => 'I agree to all terms and conditions',
                'use_hidden_element' => true,
                'checked_value' => 1,
                'unchecked_value' => 'no'
            ),
        ));

In filters, add digit validation

use Zend\Validator\Digits; // at top

$inputFilter->add($factory->createInput(array(
                        'name' => 'agreeterms',
                        'validators' => array(
                            array(
                                'name' => 'Digits',
                                'break_chain_on_failure' => true,
                                'options' => array(
                                    'messages' => array(
                                        Digits::NOT_DIGITS => 'You must agree to the terms of use.',
                                    ),
                                ),
                            ),
                        ),
                    )));
like image 197
webcoder Avatar answered Nov 05 '22 04:11

webcoder


You could also just drop the hidden form field (which I find a bit weird from a purist HTML point of view) from the options instead of setting its value to 'no' like this:

$this->add(array(
        'type' => 'Zend\Form\Element\Checkbox',
        'name' => 'agreeterms',
        'options' => array(
            'label' => 'I agree to all terms and conditions',
            'use_hidden_element' => false
        ),
    ));
like image 45
Jeff S. Avatar answered Nov 05 '22 06:11

Jeff S.


I had the same problem and did something similar to Optimus Crew's suggestion but used the Identical Validator.

If you don't set the checked_value option of the checkbox and leave it as the default it should pass in a '1' when the data is POSTed. You can set it if you require, but make sure you're checking for the same value in the token option of the validator.

$this->filter->add(array(
        'name' => 'agreeterms',
        'validators' => array(
            array(
                'name' => 'Identical',
                'options' => array(
                    'token' => '1',
                    'messages' => array(
                        Identical::NOT_SAME => 'You must agree to the terms of use.',
                    ),
                ),
            ),
        ),
    ));

This won't work if you use the option 'use_hidden_element' => false for the checkbox for the form. If you do this, you'll end up displaying the default NotEmpty message Value is required and can't be empty

like image 30
wonkenstein Avatar answered Nov 05 '22 05:11

wonkenstein