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

Skip to content

AbstractComparisonValidator does not look at root if object is null when using propertyPath #29719

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
Brammm opened this issue Dec 28, 2018 · 8 comments

Comments

@Brammm
Copy link
Contributor

Brammm commented Dec 28, 2018

Symfony version(s) affected: 4.2.x

Description
When using the Validator component with a Comparison constraint, the AbstractComparisonValidator only looks at the ExecutionContext object to compare values, not the root, see these lines: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php#L51-L53

How to reproduce
Validate an array of data with a comparison constraint:

$validator = Validation::createValidator();
$constraint = new Assert\Collection([
    'password' => [
       new Constraints\NotBlank(),
    ],
    'repeat-password' => [
       new Constraints\IdenticalTo(['propertyPath' => 'password'])
    ],
]);

$violations = $validator->validate(['password' => 'foo', 'repeat-password' => 'bar', $constraint);

Possible Solution
If the ExecutionContext object is null, use the root of the ExecutionContext to get the compared value.

@gmponos
Copy link
Contributor

gmponos commented Dec 31, 2018

possible duplicate of #28987

??

@Brammm
Copy link
Contributor Author

Brammm commented Dec 31, 2018

@gmponos I don't think so. I looked at that Issue before submitting mine, that one explicitly talks about using the form component. It's related to the same code, but I think it's a different issue.

@xabbuh
Copy link
Member

xabbuh commented Jan 10, 2019

I think the proposed solution will not work correctly as the property path would have to be adapted as soon as the array is nested in another data structure (as that makes the root of the context vary).

Thus, I don't see that the current behaviour can be changed and the we best we can do is add a hint to the docs which explains that the propertyPath option can only be used in an object context.

@Brammm
Copy link
Contributor Author

Brammm commented Jan 14, 2019

@xabbuh Hmm, yeah, that might definitely be an issue then...

@bcag2
Copy link

bcag2 commented Jun 3, 2019

Similar issue:
on stackoverflow
It seems validator search in class "Symfony\Component\Form\Form" and not in my entity where the method getAge is present !

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot carsonbot added Stalled and removed Stalled labels Dec 19, 2020
@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 Jan 2, 2021

closing for the reasons explained in #29719 (comment)

@xabbuh xabbuh closed this as completed Jan 2, 2021
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

6 participants