How to check a checkbox in ZF2

I read numerous workarounds for checking the Zend Framework by default.

I recently started using ZF2, and the documentation is a bit lacking.

Can anyone demonstrate how I can check the checkbox to make sure it has been checked with the Zend Form and Validation mechanism? I am using an array configuration for my forms (using the default setting found in the sample application on the ZF website).

+4
source share
5 answers

try it

Shape 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 the filters, add a digital check

 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.', ), ), ), ), ))); 
+18
source

You can also just drop the hidden form field (which I find a bit strange in terms of the HTML purist) from the parameters 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 ), )); 
+9
source

I had the same problem and did something similar to the Optimus Crew proposal , but used the Identical Validator.

If you have not set the checked_value option of this flag and leave it by default, it should go into the "1" field when the data is sent POSTED. You can set it if necessary, but make sure you check 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 will not work if you use the option 'use_hidden_element' => false for the checkbox for the form. If you do this, you will end up with a NotEmpty message by default Value is required and can't be empty

+3
source

This is not directly related to the question, but here are some checkbox zf2 hints if you want to save the user's response in the database ...

  • DO use the lines '1' and '0', don't worry, trying to get something else to work. In addition, you can use these values ​​directly as SQL values ​​for a bit / boolean column.
  • DO use hidden elements. If you do not, no value will be sent with the form, and no one wants it.
  • DO NOT try to filter the value to boolean. For some reason, when a boolean is false, the form is not checked, despite the presence of 'required' => false;

An example of creating an element in a form:

 $this->add([ 'name' => 'cellPhoneHasWhatsApp', 'type' => 'Checkbox', 'options' => [ 'label' => 'Cell phone has WhatsApp?', 'checked_value' => '1', 'unchecked_value' => '0', 'use_hidden_element' => true, ], ]); 

Example input filter specification:

 [ 'cellPhoneHasWhatsApp' => [ 'required' => false, ], ] 

And here is an example if you want to hide some other form fields using bootstrap:

 $this->add([ 'name' => 'automaticTitle', 'type' => 'Checkbox', 'options' => [ 'label' => 'Automatically generate title', 'checked_value' => '1', 'unchecked_value' => '0', 'use_hidden_element' => true, ], 'attributes' => [ 'data-toggle' => 'collapse', 'data-target' => '#titleGroup', 'aria-expanded' => 'false', 'aria-controls' => 'titleGroup' ], ]); 

I am a fan of ZF2, but at the end of the day you just need to find out what works with it and what does not (especially with Forms). Hope this helps someone!

+3
source

A very old question, but thought it could still be used / mentioned by people like me still using Zend Framework 2. (Using ZF 2.5.3 in my case)

Jeff's answer above helped me get the correct configuration here for what I am using. In my use case, I require a flag, but leaving it blank will be considered the value "false", which is allowed. His answer helped me allow false values, especially his:

DO NOT try to filter the value to boolean. For some reason, when a boolean is false

A use case is to enable / disable certain objects, such as countries or languages, so that they do not appear in the getEnabled[...]() Repository function.

Form element

 $this->add([ 'name' => 'enabled', 'required' => true, 'type' => Checkbox::class, 'options' => [ 'label' => _('Enabled'), 'label_attributes' => [ 'class' => '', ], 'use_hidden_element' => true, 'checked_value' => 1, 'unchecked_value' => 0, ], 'attributes' => [ 'id' => '', 'class' => '', ], ]); 

Input filter

 $this->add([ 'name' => 'enabled', 'required' => true, 'validators' => [ [ 'name' => InArray::class, 'options' => [ 'haystack' => [true, false], ], ], ], ]) 
+1
source

Source: https://habr.com/ru/post/1447038/


All Articles