-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Code property can't be populated to ConstraintViolation #7276
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
aeoris
commented
Mar 5, 2013
Q | A |
---|---|
Bug fix? | no |
New feature? | yes |
BC breaks? | no |
Deprecations? | no |
Tests pass? | yes |
Fixed tickets | #7273, #9691 |
License | MIT |
Doc PR | symfony/symfony-docs#2302 |
@@ -27,7 +27,7 @@ class BlankValidator extends ConstraintValidator | |||
public function validate($value, Constraint $constraint) | |||
{ | |||
if ('' !== $value && null !== $value) { | |||
$this->context->addViolation($constraint->message, array('{{ value }}' => $value)); | |||
$this->context->addViolation($constraint->message, array('{{ value }}' => $value), null, null, $constraint->code); |
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.
Take a look at context->addViolation()
, I think passing null as the 3rd argument is not equivalent to what was done before, there's a gotcha in the method !
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.
Oh! You mean passing $value
instead? If so, please tell me and I will change all of them to consider that! Did you detect any other issues?
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.
Yep this is what I mean (and you should add a unit test for that)
No other issue detected but I only quickly scanned it.
Ping @vicb Did my last couple commits solve the issue you pointed out? |
Looks good. "Diego Agulló" notifications@github.com wrote:
|
Done! I just don't know whether this is a new feature or a bug fix, so I selected both and made a PR to symfony-docs as well. |
you are adding a way to configure the code in the constraint. This is a new feature. I updated the description accordingly |
ping @bschussek |
@bschussek Haven't forgot this issue, and I'm happy to say that I finally have time enough to give it a go. I'm on it! |
…onCode Conflicts: src/Symfony/Component/Validator/Constraints/CountryValidator.php src/Symfony/Component/Validator/Constraints/LanguageValidator.php src/Symfony/Component/Validator/Constraints/LocaleValidator.php src/Symfony/Component/Validator/Constraints/MaxLengthValidator.php src/Symfony/Component/Validator/Constraints/MaxValidator.php src/Symfony/Component/Validator/Constraints/MinLengthValidator.php src/Symfony/Component/Validator/Constraints/MinValidator.php
@bschussek can you please check whether this PR matches #7273 now? Thanks! |
@cordoval done, thanks! |
👍 just make sure you have not missed testing each of those codes i guess? also how are you generating the codes, so that in future additions is clear how you are generating those codes. Thanks. |
@cordoval I'll double check the tests as soon as I get back home next Monday. The codes are RFC 4122-compliant 128 bit random values as proposed here by @bschussek: #7273 (comment) |
@cordoval I did indeed miss three code checks, but those are fixed now. |
@fabpot @webmozart Could you have a look at this? |
@webmozart any progress on this? |
… (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()
Replaced by #12005. |
This PR was merged into the 2.3 branch. Discussion ---------- [Validator] Simplified testing of violations | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - I simplified the assertion of violations in preparation of a replacement PR for #7276. Commits ------- 8e5537b [Validator] Simplified testing of violations
…multiple error causes (webmozart) This PR was merged into the 2.6-dev branch. Discussion ---------- [Validator] Added error codes to all constraints with multiple error causes | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #7276 | License | MIT | Doc PR | TODO This PR depends on #12015 and #12016 being merged first. However, a few changes in 52cb7df first must be backported to #12016. This PR introduces error codes for all constraints with multiple error paths. This lets you determine what exactly the reason was that a constraint failed: ```php $validator = Validation::createValidator(); $violations = $validator->validate('0-4X19-92619812', new Isbn()); foreach ($violations as $violation) { var_dump($violation->getCode()); // => int(3) var_dump(Isbn::getErrorName($violation->getCode())); // => string(24) "ERROR_INVALID_CHARACTERS" var_dump($violation->getConstraint()->getErrorName($violation->getCode())); // => string(24) "ERROR_INVALID_CHARACTERS" } ``` The `getErrorName()` method is especially helpful for REST APIs, where you can return both an error code and a description of that error now. **Todos** - [x] Backport a few structural changes to #12016 - [x] Update constraints outside of the Validator component - [x] Rebase on master (after merging #12015 and #12016) Commits ------- 3b50bf2 [Validator] Added error codes to all constraints with multiple error causes