I have some complex form, with several subforms, and I want to be able to validate each subform separately depending on radio button choosen in main form. I wanted to achieve this with validation groups.
Note: I have no data_class
model, I work with arrays.
Here is my form simplified:
class MyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('xxx', 'text', array(
'constraints' => array(
new Constraints\NotBlank(),
),
'validation_groups' => array(
'xxx',
)
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => function(FormInterface $form) {
return array('xxx');
},
));
}
}
The problem is that validation for this field is not triggered.
When this works, I can easily change setDefaultOptions
to validate desired group depending on submitted data:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => function(FormInterface $form) {
$data = $form->getData();
return array($data['type']);
},
));
}
Any idea?