-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Added 'Any' validator #11586
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
Conversation
|
||
/** | ||
* @Annotation | ||
* @Target({"PROPERTY", "METHOD", "ANNOTATION"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should also be allowed at class level
Thanks for the feedback so far, I will get it refactored and update this PR appropriately |
… (now) redundant constructor checks
…onstraint and respecting the different APIs
Ok, so I've tried to implement the creation of an Obviously the current code does not yet work as expected either, so I've tried to look at related validators; would extending the |
The 2.5 API has a |
You might be interested in #5749 which initially discussed the addition of such constraints. Given the little interest in the discussion back then I'm not sure whether this feature is worth adding. |
A different solution for this use case might be to support constraint validation in the /**
* @Assert\Expression("validate(value, Regex('/here-is-my-regex/')) or validate(value, GreaterThan(3))")
*/ maybe with support for a shortcut like /**
* @Assert\Expression("Regex('/here-is-my-regex/') or GreaterThan(3)")
*/ But then I don't know how to support constraints in custom namespace. |
@webmozart this would require registering a function for each available constraint to achieve the syntax suggested here. and we don't know this list. the expression language will not help us here, because of its restrictive syntax (which is a feature of the component) |
@stof Another alternative could be to extend the /**
* @Expression("matchesRegex or withinRange", vars = {
* "matchesRegex" = @Regex("/here-is-my-regex/"),
* "withinRange" = @GreaterThan(3),
* })
*/ PHP usage: $validator->validate($value, null, new Expression(array(
'expression' => 'matchesRegex or withinRange',
'vars' => array(
'matchesRegex' => new Regex('/here-is-my-regex/'),
'withinRange' => new GreaterThan(3),
),
))); What do you think? |
I think that the last proposal from @webmozart makes sense. |
I'll close this. Let's continue the discussion in #11940 and fix this issue accordingly. |
The
Any
constraint allows you to re-use existing constraints and is satisfied when at least one of the constraints validated.Usecase
You have a form with a field that can either be a number greater than
3
, or match a specific regex.You could write your own
Validator
class and create your own little$number > 3
-tests andpreg_match
calls. Or... you might be using an existing Constraint (e.g.Iban
), and would simply like to also allow a different value to be passed (for instance a normal bank account number).So how can we combine those constraints to satisfy a given value if any of them validates?
Here's where the
AnyValidator
comes in handy. You can pass it an array of constraints that will then be checked during validation. If any of them found the value to be valid, the constraint is satisfied (no errors).Below is an example of what this would look like in your FormBuilder:
The validation message that is used when all constraints failed can be controlled by passing it as a key/value in the
$options
argument. For example:Once this PR has reached an acceptable state I will add the necessary docs to complement the changes.