How to Define the Validation Groups to Use
Warning: You are browsing the documentation for Symfony 3.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.2 (the current stable version).
Validation Groups
If your object takes advantage of validation groups, you'll need to specify which validation group(s) your form should use:
1 2 3
$form = $this->createFormBuilder($user, [
'validation_groups' => ['registration'],
])->add(...);
If you're creating form classes (a good
practice), then you'll need to add the following to the configureOptions()
method:
1 2 3 4 5 6 7 8
use Symfony\Component\OptionsResolver\OptionsResolver;
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'validation_groups' => ['registration'],
]);
}
In both of these cases, only the registration
validation group will
be used to validate the underlying object. To apply the registration
group and all constraints that are not in a group, use:
1
'validation_groups' => ['Default', 'registration']