-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Code property can't be populated to ConstraintViolation #7273
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
Why do you need this new property? Can you show me a problem that can't be solved with the currently existing functionality? |
@bschussek What is the goal of the code in the ConstraintViolation if it is never set ? |
@bschussek that's exactly the point, what @stof already mentioned. We now have an existing property that can't be populated via the usual ways (annotations et al). I explained my particular needs in StackOverflow (the question is linked in the first message): I need to provide a constraint error code in addition to the violation message. It will allow an API to check which error was thrown comparing against an integer instead of the message - of course, I want both fields, not just one, as the message is nice to have and it may change for different users (i18n). |
@stof The point of the code property is to programmatically analyze where a violation comes from. Currently this is only used for "invalid" errors on forms. One enhancement I see is to add a if (NotNull::CODE === $violation->getCode()) For constraints that have multiple error scenarios, multiple if (Length::CODE_MIN === $violation->getCode()) But for me it doesn't make sense to let the definition of the constraint set the code of the potential error. Imagine there is a generic module that relies on codes being set as in the examples I just gave. If you change the code in the constraint definition, this module will break. @aeoris Would this solution also be acceptable for you? |
Obviously, all of these constants need to contain some sort of UUID. |
@bschussek Perfectly valid, indeed. As long as there's a way to compare against a constant value, that's fine for my use case. I see your point on possible dependencies between modules, and I haven't thought about that. |
Ok. Before we implement this, let's work on the naming first. I think Alternatives: if (NotNull::CODE === $violation->getCode())
if (Length::CODE_MIN === $violation->getCode())
if (NotNull::ERROR === $violation->getCode())
if (Length::ERROR_MIN === $violation->getCode())
if (NotNull::ERR === $violation->getCode())
if (Length::ERR_MIN === $violation->getCode())
if (NotNull::VIOLATION === $violation->getCode())
if (Length::VIOLATION_MIN === $violation->getCode()) Which naming do you prefer? |
My preference would probably be if (NotNull::ERROR === $violation->getCode())
if (Length::ERROR_MIN === $violation->getCode()) or even if (NotNull::ERROR === $violation->getCode())
if (Length::MIN_ERROR === $violation->getCode()) |
I would go with PHP's own (sort of) convention, although I don't know if it matches Symfony's: if (NotNull::E_CODE === $violation->getCode())
if (Length::E_CODE_MIN === $violation->getCode()) Otherwise, I also like your first preference. |
@aeoris But to be really consistent with this convention it should probably be if (NotNull::E_ERROR === $violation->getCode())
if (Length::E_MIN_ERROR === $violation->getCode()) |
True. A possible drawback can be that |
Ok, let's go for that solution but without the |
Sure! What about the possible values? Shall we go with UUID, 2^n, a regular incremental int? |
As mentioned before, every constraint that may result in an error should receive either one I suggest to use 128bit random values generated according to the Leach-Salz algorithm (RFC4122, as in Java) as values. For an online UUID generator with this algorithm, check http://www.famkruithof.net/uuid/uuidgen and set "type" to "Version 4: Random". |
… (webmozart) This PR was merged into the 2.6-dev branch. Discussion ---------- [Validator] Added ConstraintViolation::getConstraint() | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #5050 | License | MIT | Doc PR | - This PR adds a `getConstraint()` method to the `ConstraintViolation` in order to access the constraint causing the violation. Related to #7276, #7273 and #9691. Commits ------- ce1d209 [Validator] Added ConstraintViolation::getConstraint()
#11657 was just merged. That means you can now access the constraint that caused a violation: $violation->getConstraint() The only thing that is missing now is to access the reason for constraints that can have multiple error paths (min, max, etc.). |
…for attaching domain-specific data (webmozart) This PR was merged into the 2.6-dev branch. Discussion ---------- [Validator] Added "payload" option to all constraints for attaching domain-specific data | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #7273 | License | MIT | Doc PR | TODO The "payload" option can be used to pass whatever data should be attached to a constraint for an application: ```php /** * Domain-specific error codes * @NotNull(payload="100") */ /** * Structured domain-specific data * @NotNull(payload={"display": "inline", "highlight": false}) */ ``` The term "payload" is borrowed from JSR-303. Commits ------- e8b7c6d [Validator] Added "payload" option to all constraints for attaching domain-specific data
Following my (unanswered) question at StackOverflow I'm reporting this as a bug, as no one seems to know the answer.
Currently there's no way that I can see to populate the
code
property for theConstraintViolation
class, as it is not defined in theConstraint
class. But as I see, it goes farther than that, as all the validators just ignore the parameters for theaddViolation
method, making it impossible for simple asserts to define a code for the violation.What I expect it to allow is the following:
Problem is, the current validators call
addViolations
without setting the 5th argument (code
). Plus, theConstraint
class doesn't implement such acode
property as mentioned.If this is indeed considered a bug, I would like to send a PR with a patch, provided that this is the way that it should be working.If it's not, some directions would be amazing!
The text was updated successfully, but these errors were encountered: