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

Skip to content

[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

Closed
Seb33300 opened this issue Aug 2, 2019 · 12 comments
Closed

[Validator] Add a constraint condition option #32877

Seb33300 opened this issue Aug 2, 2019 · 12 comments

Comments

@Seb33300
Copy link
Contributor

Seb33300 commented Aug 2, 2019

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 the Expression 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:

/**
 * @Assert\Expression("value or this.isNullable()", message="This value should be null.")
 */
protected $nullableOnCondition;

We will be able to do:

/**
 * @Assert\NotNull(condition="!this.isNullable()")
 */
protected $nullableOnCondition;
@Seb33300 Seb33300 changed the title [Validator] Constraint condition option [Validator] Add a constraint condition option Aug 2, 2019
@xabbuh
Copy link
Member

xabbuh commented Aug 2, 2019

This is something that is already possible using validation groups. I am not convinced that we need yet another way to achieve the same.

@Seb33300
Copy link
Contributor Author

Seb33300 commented Aug 3, 2019

I know we can use validation groups, but I am always trying to avoid to use them.
I don't really like the way they work. We cannot know what are the required conditions for a validation group to be used just by looking at them. We need to find in the code where they are used to understand their purpose.

If I take another example:
A form providing a subject select dropdown with an other choice.
If the other choice is selected, the subject_other text field become required and must be NotBlank.

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 NotBlank constraint attached to the subject_other field and it's done.

It's just more or less the same as using an Expression constraint.

@felipyamorim
Copy link
Contributor

I like the idea, but, as a workaround i created a custom constraint to do it.

@AppAssert\IfThen(condition="this.getFoo()", constraints={@Assert\NotBlank()})

@vudaltsov
Copy link
Contributor

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

@BoShurik
Copy link
Contributor

@vudaltsov it would be great to see this in the core.

I have a price object and want it to be valid if value property is not empty. I think it's a common practice to work with "empty" object and it's easily solved with your Condition constraint.

/**
 * @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

@gmponos
Copy link
Contributor

gmponos commented Aug 21, 2019

I have a price object and want it to be valid if value property is not empty.

Your constraints should have this logic inside of them and not extracted as a parameter..

@walva
Copy link
Contributor

walva commented Aug 19, 2020

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

@xabbuh
Copy link
Member

xabbuh commented Aug 20, 2020

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.

@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@carsonbot
Copy link

Friendly reminder that this issue exists. If I don't hear anything I'll close this.

@xabbuh
Copy link
Member

xabbuh commented Mar 7, 2021

Let's close here for now. We can consider to reopen once there is a well-working third-party solution that could be integrated.

@xabbuh xabbuh closed this as completed Mar 7, 2021
@drzraf
Copy link

drzraf commented May 14, 2021

Isn't @vudaltsov idea (see his gist) worth a PR?
Seems simple and powerful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants