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!
source share