Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] Add AtLeastOne constraint and validator #35744

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

Merged
merged 1 commit into from
Mar 16, 2020

Conversation

przemyslaw-bogusz
Copy link
Contributor

Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? no
License MIT
Doc PR TODO

This constraint allows you to apply a collection of constraints to a value, and it will be considered valid, if it satisfies at least one of the constraints from the collection.

Some examples:

    /**
     * @Assert\AtLeastOne({
     *     @Assert\Length(min=5),
     *     @Assert\EqualTo("bar")
     * })
     */
    public $name = 'foo';

    /**
     * @Assert\AtLeastOne({
     *     @Assert\All({@Assert\GreaterThanOrEqual(10)}),
     *     @Assert\Count(20)
     * })
     */
    public $numbers = ['3', '5'];

    /**
     * @Assert\All({
     *     @Assert\AtLeastOne({
     *          @Assert\GreaterThanOrEqual(5),
     *          @Assert\LessThanOrEqual(3)
     *     })
     * })
     */
    public $otherNumbers = ['4', '5'];

The respective default messages would be:
name: This value should satisfy at least one of the following constraints: [1] This value is too short. It should have 5 characters or more. [2] This value should be equal to "bar".

numbers: This value should satisfy at least one of the following constraints: [1] Each element of this collection should satisfy its own set of constraints. [2] This collection should contain exactly 20 elements.

otherNumbers[0]: This value should satisfy at least one of the following constraints: [1] This value should be greater than or equal to 5. [2] This value should be less than or equal to 3.

But of course you could also create a simple custom message like None of the constraints are satisfied.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks
Can you add more tests please? With several constraints, both passing and not passing.

@fancyweb
Copy link
Contributor

I am not sure about this one because what if I want "at least 2" or "at most 3", etc. I think what you want to achieve is a composition of constraints (ref #35226), right?

When we need a composition of different constraints, shouldn't we create a custom constraint reusing core constraints "in context" instead?

What about maybe finding another name and add an option to choose the number of constraints that must be valid, so it is at least a bit dynamic?

@nicolas-grekas
Copy link
Member

Before increasing the complexity we should be sure there is a need for the feature.

@przemyslaw-bogusz
Copy link
Contributor Author

When I started working on this one, I thought about creating a parameter for the number of "internal" constraints, that have to be satisfied. So initially, it was the AtLeast constraint. But with all the discussion around color validators, I decided, that a one-of-many variant should cover 99% of cases, in which anyone would ever wanted to use such a constraint. Nevertheless, making it the AtLeast constraint with default option set to one, isn't such a bad idea.

By the way, I will add more tests soon.

@nicolas-grekas
Copy link
Member

I don't think AtLeast > 1 serves any use case.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add more tests please? With several constraints, both passing and not passing.

@fabpot fabpot force-pushed the at_least_constraint branch from f4acb0e to e6209a6 Compare March 16, 2020 06:29
@fabpot
Copy link
Member

fabpot commented Mar 16, 2020

Thank you @przemyslaw-bogusz.

@fabpot fabpot merged commit b4f03d0 into symfony:master Mar 16, 2020
@przemyslaw-bogusz przemyslaw-bogusz deleted the at_least_constraint branch March 16, 2020 20:29
fabpot added a commit that referenced this pull request Mar 23, 2020
This PR was squashed before being merged into the 5.1-dev branch.

Discussion
----------

[Validator] Add missing translations

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| License       | MIT

I forgot about it in the original PR (#35744) - sorry for the extra work.

Commits
-------

0c06856 [Validator] Add missing translations
@nicolas-grekas nicolas-grekas removed this from the next milestone May 4, 2020
@nicolas-grekas nicolas-grekas added this to the 5.1 milestone May 4, 2020
@fabpot fabpot mentioned this pull request May 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants