There is a complex form with a lot of nested fieldsets. Some fields need to be validated depending on field(-s) in another fieldset. So I cannot define all the validation rules directly in the getInputFilterSpecification()
of the Fieldset
, since there I cannot access other fieldsets (only the sub-fieldsets). The only way to do this is to extend the Form
validation. Right? If so, how to do this?
MyForm extends Form
{
public function isValid()
{
$isFormValid = parent::isValid();
$isFieldXyzValid = // my additional validation logic
if(! $isFieldXyzValid) {
$fieldXyz->setMessages(...);
}
return $isFormValid && $isFieldXyzValid;
}
}
Like this? Or is a cleaner way to solve this problem?
I've already developed something similar in my previous project.
For doing this i used a service which take my form and set dynamic fieldset, and obviously custom validation rules.
In your controller, get your form (via the dependancy injection formManager (polyfilled or not).
Call your service and give it your form.
And in your service you can do anything you want like :
I performed those via (sample) in a foreach where
$stuff
is an element of doctrine collectionWhere setFilters and setValidators are custom methods wich add filters and validator to my fields (also custom)