By default a multicheckbox element triggers an error message when none of the options are selected:
array (size=1)
'checkbox' =>
array (size=1)
'isEmpty' => string 'Value is required and can't be empty' (length=36)
I found out that the message can be disabled using the function getInputFilterSpecification:
class MyForm extends Form implements InputFilterProviderInterface{
private $inputFilter;
public function __construct($name = null){
parent::__construct($name);
$this->add([
'name' => 'checkbox',
'type' => 'checkbox',
'attributes' => [],
'options' => [
'value_options' =>[
[
'value' => '0',
//'selected' => true,
'label' => 'One',
'label_attributes'=>[],
'attributes' => [],
],
[
'value' => '1',
'label' => 'Two',
'label_attributes'=>[],
'attributes' => [],
],
[
'value' => '2',
'label' => 'Three',
'label_attributes'=>[],
'attributes' => [],
],
],
],
]);
}//construct
public function getInputFilterSpecification() {
return array(
'checkbox'=>['required'=>false,],
);
}
}//class
The only "issue" with this approach is that you have to specify name and the relative option for all the instances of the multicheckbox element; having 10 different multicheckboxes the inputfilter specifications would be something like:
public function getInputFilterSpecification() {
return array(
'checkbox1'=>['required'=>false,],
'checkbox2'=>['required'=>false,],
'checkbox3'=>['required'=>false,],
...
);
}
...and ok it's not that bad but it seems pretty dumb since the element (and/or all the value options) accepts the attribute required but then is practically treated as if it was required and that makes no sense at all.
Where does that message come from? It seems a preassigned notEmpty validator but I haven't found any trace of it in the Zend's Mulicheckbox class or into the parent Checkbox class.
Isn't there really no better approach to do that?
I found the answer on my own.
Zend's Multicheckbox element extends the Checkbox element which implements
InputProviderInterface.And in Checkbox (line 165) there is:
That required parameter is retrieved from the
Zend\InputFilter\Inputclass and if true (always true) a notEmpty validator is added.Ergo: there's no way to change that behaviour throug the element's options (and again: that's dumb).
Anyway, something can be done by using a custom element that extends multicheckbox (and that's actually what I'm already doing, for other reasons):
I just copied the
getInputSpecificationfunction from the original Checkbox element and set required to false. This way there's no need anymore to specify the required parameter into the form'sgetInputFilterSpecificationfunction.Obviously this solution makes sense if you're already using a custom version of multicheckbox, otherwise...make your own considerations.
UPDATE: handle the element's required attribute, practical use
Basically you will handle the required attribute this way (here for a custom Radio element):
Then:
Alternatively you might want to assign the required attribute to a few options only (senseless for a radio element):
Then:
And finally:
Note: depending on how you render your custom elements you might want to disable browser's validation by adding the novalidate attribute to your form: