Skip to content

Include option in validator to validate all groups #49932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dwgebler opened this issue Apr 4, 2023 · 0 comments · May be fixed by #50679
Open

Include option in validator to validate all groups #49932

dwgebler opened this issue Apr 4, 2023 · 0 comments · May be fixed by #50679

Comments

@dwgebler
Copy link

dwgebler commented Apr 4, 2023

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']);
@dwgebler dwgebler changed the title Include option in validator to validate all grous Include option in validator to validate all groups Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants