Skip to content

[Validator] Add the errorPath option to the Expression constraint #41921

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

Conversation

sidz
Copy link
Contributor

@sidz sidz commented Jun 30, 2021

Q A
Branch? 5.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets N/A
License MIT
Doc PR @todo

PR adds the errorPath option to the Expression constraint.
This option can be useful for cases when Expression constraint is applied on the Class Level and we need to define the error path.
Current behavior put an empty string to the propertyPath.

It works in the same way like in the Unique constraint.

@carsonbot
Copy link

Hey!

I think @lmasforne has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@nicolas-grekas nicolas-grekas added this to the 5.4 milestone Jul 2, 2021
@@ -44,6 +44,7 @@ public function validate($value, Constraint $constraint)

if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
$this->context->buildViolation($constraint->message)
->atPath((string) $constraint->errorPath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the cast to string is not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sidz sidz force-pushed the control-expression-constraint-error-path branch from 795c385 to a5f396d Compare July 2, 2021 09:54
@@ -65,6 +67,7 @@ public function __construct(

$this->message = $message ?? $this->message;
$this->values = $values ?? $this->values;
$this->errorPath = $errorPath ?? $this->errorPath;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->errorPath = $errorPath; is enough here a well

@@ -44,6 +44,7 @@ public function validate($value, Constraint $constraint)

if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
$this->context->buildViolation($constraint->message)
->atPath($constraint->errorPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this should be called only when $constraint->errorPath !== null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or cast to string which was originally added. So what would be the best way?

cc/ @nicolas-grekas

@ogizanagi
Copy link
Contributor

ogizanagi commented Jul 5, 2021

This option can be useful for cases when Expression constraint is applied on the Class Level and we need to define the error path.

Is this needed ? Since you can already define the expression on the property for which you want the violation message to be associated with while still accessing the class context when using this in the expression.

(see the "Mapping the Error to a Specific Field" section from https://symfony.com/doc/current/reference/constraints/Expression.html)

@sidz
Copy link
Contributor Author

sidz commented Jul 6, 2021

@ogizanagi I see your point but let say I'm building an API and I need to validate smth against class and return violation property path equal to __root__ ? Currently it is not possible to achieve this and as a developer I need to create a custom constraint but it'd be useful to do via Expression constraint.

Based on your suggestion I'd say that Target = Class is not needed an need to be removed from Expression constraint.

@sidz sidz force-pushed the control-expression-constraint-error-path branch 3 times, most recently from fd7fb14 to 06fdea0 Compare July 12, 2021 13:18
@sidz sidz force-pushed the control-expression-constraint-error-path branch from 4dc63c0 to 3a79485 Compare July 12, 2021 13:27
@ogizanagi
Copy link
Contributor

Based on your suggestion I'd say that Target = Class is not needed an need to be removed from Expression constraint.

I don't think so, since it's still valid to have the expression and error mapped to the class, as it'll be exposed to another object property when embedded.

@sidz sidz force-pushed the control-expression-constraint-error-path branch from 6df3f61 to 3a79485 Compare July 12, 2021 13:50
@sidz
Copy link
Contributor Author

sidz commented Jul 12, 2021

looks like CI is failed due to unrelated to this PR reason

@fabpot fabpot modified the milestones: 5.4, 6.1 Nov 16, 2021
@sidz
Copy link
Contributor Author

sidz commented Dec 4, 2021

Should I rebase my branch against 6.1 as milestone 5.4 has been replaced by 6.1?

@fabpot fabpot modified the milestones: 6.1, 6.2 May 20, 2022
@nicolas-grekas nicolas-grekas modified the milestones: 6.2, 6.3 Nov 5, 2022
@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@sidz sidz closed this by deleting the head repository Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants