Validate a Check Box - Zend Framework 2 (ZF2)

If you have a registration form with a terms and conditions check box you'll probably want to validate that it is checked. This can be accomplished by using the Digits Validator.

Start by defining your input as follows: ...

Notice that the checked_value = 1.
Notice also that the unchecked_value = 'no'.
Look at the Zend\Validator\Digits class and notice that it validates the following:

The check box will fail validation when it is 'unchecked' because it contains the alpha characters ('no'). So next we create our input filter for our terms check box and give the 'NOT_DIGITS' our custom message.

I am still a little bummed out that decorators are no longer used but am impressed with some of the new capability provided by ZF2.

Master Control Program:  You're in trouble, program. Why don't you make it easy on yourself. Who's your user? 

Validate a Check Box - Zend Framework 2 (ZF2)

1 comment:

  1. How will you display the error message passed by the Zend\Validator\Digits in the form you submitted?