Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

przemyslaw-bogusz
Copy link
Contributor

QA
Branch?master
Bug fix?no
New feature?yes
Deprecations?no
LicenseMIT
Doc PRTODO

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 likeNone of the constraints are satisfied.

OskarStark and luukvankooten reacted with thumbs up emoji
Copy link
Member

@nicolas-grekasnicolas-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
ContributorAuthor

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 theAtLeast 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 theAtLeast 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.

ogizanagi reacted with thumbs up emoji

Copy link
Member

@nicolas-grekasnicolas-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
Copy link
Member

Thank you@przemyslaw-bogusz.

@fabpotfabpot merged commitb4f03d0 intosymfony:masterMar 16, 2020
@przemyslaw-boguszprzemyslaw-bogusz deleted the at_least_constraint branchMarch 16, 2020 20:29
fabpot added a commit that referenced this pull requestMar 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       | MITI forgot about it in the original PR (#35744) - sorry for the extra work.Commits-------0c06856 [Validator] Add missing translations
@nicolas-grekasnicolas-grekas removed this from thenext milestoneMay 4, 2020
@nicolas-grekasnicolas-grekas added this to the5.1 milestoneMay 4, 2020
@fabpotfabpot mentioned this pull requestMay 5, 2020
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ogizanagiogizanagiogizanagi left review comments

@norkunasnorkunasnorkunas left review comments

@fabpotfabpotfabpot approved these changes

@OskarStarkOskarStarkOskarStark approved these changes

@nicolas-grekasnicolas-grekasAwaiting requested review from nicolas-grekas

Assignees
No one assigned
Projects
None yet
Milestone
5.1
Development

Successfully merging this pull request may close these issues.

8 participants
@przemyslaw-bogusz@fancyweb@nicolas-grekas@fabpot@OskarStark@ogizanagi@norkunas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp