-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] wrong constraint attached to ConstraintViolation #20368
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
Comments
I am not sure if I fully understood your problem. In your test case, what did happen and what did you actually expect to happen instead? |
In case of a nested (composite) constraints, specifically the I have updated the gist with comments. |
Your issue actually is that you assign an empty array to the |
I did that in the testcase to better illustrate the problem. The constraint returned is wrong no matter what I do. See the 2nd test. |
I missed your second test case. This is indeed an issue. |
For the records, this is related to #14786. |
…ns (xabbuh) This PR was merged into the 2.7 branch. Discussion ---------- [Validator] ensure the proper context for nested validations | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #14639, #20368 | License | MIT | Doc PR | n/a Commits ------- 56c8ff8 ensure the proper context for nested validations
Sorry for the late response. Yes, it solves the issue (2nd test passes). The first test still fails although as you noticed I use empty key (without constraints) which is unsupported. |
Thank you very much for the confirmation @pinkeen! |
I realized I have pulled wrong version of the validator component. Retested the proper version. Both tests pass, even with the key without constraints. Tried few other variations - changing order, changing the validators - all green! Thank you for the fix! |
Version: symfony/validator 3.1.6, PHP 7.0.7
Background: I've been using the validator component for APIs for a long time (without forms though). One of the problems was that apart from the descriptive violation messages I wanted to return a fixed error name that can be handled by the application on the other end. I've achieved that by extending the base constraints and overriding the error messages (which means no descriptive message). I could live with that, waited for better times.
I noticed that the ConstraintViolation includes the
::getCode
method now! Perfect for my little APIs. The code seems to be an UUID and each of the constraints includes a mapping to nice error names. UUIDs are usable but they are not the best UX for the end-programmer :).The meat: The easiest way to get the error name would be to use
Constraint::getErrorName
method. This is where the problem is - in some cases theConstraintViolation
has the wrongConstraint
attached. From the first look - it's the last tested constraint, not the one that failed.I am aware that I am assuming the the
ConstraintViolationInterface
is actually aConstraintViolation
but IMO it's fair to assume when I'm using Symfony's validator component. Also theConstraintViolation::getConstraint
andConstraint::getErrorName
methods are public so I am (I hope) not relying on some internal implementation details.I am attaching a testcase for further illustration:
https://gist.github.com/pinkeen/7a3b6688efc3c493fc16807ac89d3a3f
The text was updated successfully, but these errors were encountered: