-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Add "is_valid" function to the Expression constraint #33829
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
69ecdc4
to
324a374
Compare
return; | ||
} | ||
|
||
$this->expressionLanguage->register('is_valid', function () { |
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.
This code should be moved to a dedicated class ExpressionLanguageProvider
by implementing ExpressionFunctionProviderInterface
and later $this->expressionLanguage->registerProvider(...)
.
The API feals really strange, I would expect that calling We could expose an API similar to the With this API your use cases would become: /**
* Constraints:
* @Expression("is_valid(value, a) or is_valid(value, b)", values={"a": @Type('string'), "b": @Type('int')})
*
* Properties:
* @Expression("not is_valid(value.foo) or not is_valid(value.bar)")
*/ It's a bit more verbose but I think it's a lot more clear what is validated and against which constraints. |
I agree it's more understandable but the first argument will always be
Not possible because the function would get the resolved value, not the property. The passed arg has to be a string. |
I don't see why it shouldn't be allowed.
That's what I wanted to do, the function would get the resolved value and validated it. But I guess what you wanted is validate it against the constraints defined in the object (the same way Maybe we could allow a |
@jvasseur After some more reflexion I think you are right:
For objects we can but for anything else, there would be no constraints to check against, isn't it?
Yes!
Or we can just have another function |
This PR was merged into the 4.4 branch. Discussion ---------- [Validator] Fix wrong expression language value | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - https://github.com/symfony/symfony/blob/766162c4c790cc0b91185963b9c36b842bf2eb49/src/Symfony/Component/Validator/Constraints/ExpressionValidator.php#L28 ```php (new ExpressionValidator($propertyAccessor))->validate($object, $constraint); ``` Based on the previous method signature (4.3 above), that code would result in an exception in 4.4: ``` Call to undefined method Symfony\Component\PropertyAccess\PropertyAccessor::evaluate() ``` Spotted by @fancyweb in #33829 (comment) Fixed here and updated test case to avoid regression. Commits ------- 4288f1c Fix wrong expression language value
@fancyweb I see that you changed your mind about how to solve the problem. So you want to update this PR accordingly? |
I need to refocus on this work as I left it "on the side". I'm closing and I will maybe reopen a new PR in the future. |
The main benefit of this feature is to be able to support "or" constraints but it generally open the usage of this constraint to many cool cases.
is_valid
accepts an unlimited number of arguments of 3 kinds: