-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Add the When
constraint and validator
#42593
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
Hey! I see that this is your first PR. That is great! Welcome! Symfony has a contribution guide which I suggest you to read. In short:
Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change. When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor! I am going to sit back now and wait for the reviews. Cheers! Carsonbot |
28c2de6
to
965f556
Compare
Hey! I think @lmasforne has recently worked with this code. Maybe they can help review this? Cheers! Carsonbot |
Actually there is no |
How does it play with PHP Attribute? |
@lyrixx |
what about |
PHP 8.1. The future is now. 😉 See also #41994 |
Looks related to #32877 |
I don't have a preference for the name. 1️⃣ |
e5f889f
to
523e13f
Compare
{ | ||
if (null === $this->expressionLanguage) { | ||
if (!class_exists(ExpressionLanguage::class)) { | ||
throw new LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.'); |
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.
throw new LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.'); | |
throw new LogicException(sprintf('The "symfony/expression-language" component is required to use the "%s" constraint.', __CLASS__)); |
Maybe move this check to Conditional
's constructor like it's done for Expression
?
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.
There is already the check in the constructor.
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.
Indeed. This makes this check unnecessary then, because you can't reach this line without an instance of Conditional
, which would have already performed the same check via its constructor.
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.
Actually we can reach this line with an instance of a class that extends Conditional
and with different constructor.
I get it won't almost never happen but it seems logic in the context of this validator class.
To be honest, I took this check from
symfony/src/Symfony/Component/Validator/Constraints/ExpressionValidator.php
Lines 74 to 76 in e0cadfb
if (!class_exists(ExpressionLanguage::class)) { | |
throw new LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.'); | |
} |
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.
Right. Maybe the constraint class could be made final
?
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.
I don't think I should make it final
because it will make the class non extendable for further implementation.
src/Symfony/Component/Validator/Tests/Constraints/ConditionalTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Tests/Constraints/ConditionalTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Tests/Constraints/ConditionalValidatorTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Tests/Constraints/ConditionalValidatorTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Tests/Constraints/ConditionalValidatorTest.php
Outdated
Show resolved
Hide resolved
523e13f
to
8c0f9ea
Compare
Oh I forgot, +1 for |
6097bee
to
56de60c
Compare
@maxhelias |
56de60c
to
e265ad5
Compare
974e2de
to
e265ad5
Compare
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.
LGTM (except for the minor comment)
e265ad5
to
ab07c8b
Compare
ab07c8b
to
421520f
Compare
When
constraint and validator
421520f
to
9b7bdc9
Compare
Thank you @wuchen90 and congratulations to your first Symfony contribution! |
This constraint allows you to apply constraints validation only if the provided condition is matched.
Usage:
See the documentation for details.