I'm using a lot of nested Fieldset and was wondering if is it possible to set a required=false to a specific fieldset in order to not throw an error if there is any in it (and keep my form validating) ?
For example this is my UserFieldset :
-> {User Fieldset}
-----> Name
-----> Email address
-----> [other user fields...]
-----> {Avatar Fieldset}
----------> File
----------> [other avatar fields...]
-----> {other fieldsets...}
In my situation I want sometimes to force my user to add an avatar and sometimes not..
When I want to force there is no problem, my input file in my Avatar fieldset throws an error and blocks the validation process.
But when I want my user to simply register and eventually set its avatar, how can I do it ?
Thank you for your help.
You can modify the elements that are validated using the setValidationGroup()
method.
Zend\Form provides a proxy method to the underlying InputFilter‘s setValidationGroup() method, allowing us to perform [validation] on only a subset of form elements
This could be done within the controller or perhaps a new factory; for example:
// Module.php
public function getFormElementConfig()
{
return array(
'factories' => array(
'MyModule\Form\AvatarRegistrationFieldset' => function($fem) {
$fieldset = new Form\AvatarFieldset();
// Include the name of the elements you WANT to be validated
$fieldset->setValidationGroup('foo', 'file');
return $fieldset;
}
),
);
}