-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Add a constraint condition option #32877
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
Comments
This is something that is already possible using validation groups. I am not convinced that we need yet another way to achieve the same. |
I know we can use validation groups, but I am always trying to avoid to use them. If I take another example: Using validation groups, this means we have to set up validation groups based on submitted data in every forms we are using the fields. With the suggested feature, we just need to add a condition on a It's just more or less the same as using an |
I like the idea, but, as a workaround i created a custom constraint to do it.
|
@Seb33300 , indeed, groups are often appear to be cumbersome. Agree with @felipyamorim , composition of constraints is a much better idea than an option in every single constraint. Made a similar one back in the days, see https://gist.github.com/vudaltsov/8e652c122e2344ce8574635b5514a975. I think I will do a package for this with a couple of other usefull composition constraints. And then we'll see if it makes sense in the core. |
@vudaltsov it would be great to see this in the core. I have a price object and want it to be valid if /**
* @var PriceModel
*
* @Condition("this.price and this.price.value", {
* @Assert\Valid()
* })
*/
public $price; Correct me if I'm wrong but looks like it's impossible to solve this with validation group |
Your constraints should have this logic inside of them and not extracted as a parameter.. |
I found another situation where this feature request can help. At first, I thought like @xabbuh , groups are enough. Especially if you consider this: If residence country == 'BE', national registration number is required Simple right? class Person {
/** @NotBlank(groups=["BE"]) */
public $nationalRegistrationNumber;
}
$validator->validate($data, null, $data['country'] == 'BE' ? ['BE'] : []); This is fine. But, how would you implement this: if country <> 'BE', passport is required I can array_filter(Country::all(), function($country) use ($excludedCountries) { return $country not in ... But yeah, the proposal seems legit to me :D |
I like the proposal from #32877 (comment). The advantage is being able to have this in a different package first as suggested in #32877 (comment) thus being able to evaluate first if it proves useful. |
Thank you for this suggestion. |
Friendly reminder that this issue exists. If I don't hear anything I'll close this. |
Let's close here for now. We can consider to reopen once there is a well-working third-party solution that could be integrated. |
Isn't @vudaltsov idea (see his gist) worth a PR? |
Description
I would like to request a new way to handle when a constraint should be applied or not by adding a new
condition
option on all constraints.The new
condition
option should accept an expression in the same way as theExpression
constraint.Thanks to this option, we should be able to use a more appropriated constraint than the
Expression
constraint when possible.Example
For example, it's easier to understand what a
NotNull
constraint is intended to.Instead of doing:
We will be able to do:
The text was updated successfully, but these errors were encountered: