Description
Description
https://symfony.com/doc/current/validation/groups.html
Validation groups allows us to define groups on individual properties of objects as per the following documented example:
class User implements UserInterface
{
#[Assert\Email(groups: ['registration'])]
private $email;
// $city belongs to special group "Default"
#[Assert\Length(min: 2)]
private $city;
We can then validate the registration group specifically by calling $validator->validate($author, null, ['registration']);
, or we can validate any properties which do not have an explicit group configured by using the group name Default
or not specifying a group name, e.g. $validator->validate($author, null, ['Default']);
, or just $validator->validate($author);
But at the moment, if we want to validate both groups, we'd have to use $validator->validate($author, null, ['Default', 'registration']);
This is fine when you only have maybe two or three groups to work with, including Default
, but gets messy when you have an arbitrarily larger number of groups.
At the moment, if you want to easily validate all groups, there is no built-in option for doing this except to either list every group in the call to validate()
, or to add a group name such as all
to every single property.
Would be nice if we could do $validator->validate($author, null, ['All']);
or similar as a special group name which automatically applied validation to all properties, regardless of groups.
Example
class User
{
#[Assert\Email(groups: ['registration'])]
private $email;
#[Assert\Length(min: 2, groups:['personal'])]
private $firstName;
#[Assert\Length(min: 2)]
private $city;
}
$errors = $validator->validate($user, null, ['All']);