Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

[Validator] Code property can't be populated to ConstraintViolation #7276

Closed
wants to merge 7 commits into
from

Conversation

Projects
None yet
7 participants
Contributor

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

@vicb vicb and 1 other commented on an outdated diff Mar 6, 2013

...ny/Component/Validator/Constraints/BlankValidator.php
@@ -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);
@vicb

vicb Mar 6, 2013

Contributor

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 !

@aeoris

aeoris Mar 6, 2013

Contributor

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?

@vicb

vicb Mar 6, 2013

Contributor

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.

Contributor

aeoris commented Mar 12, 2013

Ping @vicb

Did my last couple commits solve the issue you pointed out?

Contributor

vicb commented Mar 13, 2013

Looks good.
Please use the standard pr header (found on symfony.com). Thanks.

"Diego Agulló" notifications@github.com wrote:

Ping @vicb

Did my last couple commits solve the issue you pointed out?


Reply to this email directly or view it on GitHub:
#7276 (comment)

@aeoris aeoris added a commit to aeoris/symfony-docs that referenced this pull request Mar 13, 2013

@aeoris aeoris Add information on code property population 687fea6

@aeoris aeoris referenced this pull request in symfony/symfony-docs Mar 13, 2013

Closed

[WCM] Add information on code property population #2302

Contributor

aeoris commented Mar 13, 2013

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.

Member

stof commented Mar 19, 2013

you are adding a way to configure the code in the constraint. This is a new feature. I updated the description accordingly

Owner

fabpot commented Sep 13, 2013

ping @bschussek

Contributor

webmozart commented Sep 16, 2013

@fabpot The implementation of this PR doesn't match the specification in #7273.

@aeoris Do you have time to update this PR?

Contributor

aeoris commented Nov 2, 2013

@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!

aeoris added some commits Nov 2, 2013

@aeoris aeoris Merge remote-tracking branch 'upstream/master' into ConstraintViolati…
…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
e2eca8e
@aeoris aeoris Added unique error codes on constraints (#7273, #7276, #5525). 82249ff
Contributor

aeoris commented Nov 8, 2013

@bschussek can you please check whether this PR matches #7273 now? Thanks!

Contributor

cordoval commented Dec 6, 2013

@aeoris could you please add to the description table that it fixes #9691 please ? 👶

Contributor

aeoris commented Dec 6, 2013

@cordoval done, thanks!

Contributor

cordoval commented Dec 21, 2013

👍 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.

Contributor

aeoris commented Dec 23, 2013

@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)

Contributor

aeoris commented Feb 15, 2014

@cordoval I did indeed miss three code checks, but those are fixed now.

Contributor

stloyd commented Mar 11, 2014

@fabpot @webmozart Could you have a look at this?

Contributor

aeoris commented May 23, 2014

@webmozart any progress on this?

@webmozart webmozart added a commit that referenced this pull request Aug 19, 2014

@webmozart webmozart feature #11657 [Validator] Added ConstraintViolation::getConstraint()…
… (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()
2293556
Contributor

webmozart commented Sep 23, 2014

Replaced by #12005.

@webmozart webmozart closed this Sep 23, 2014

@fabpot fabpot added a commit that referenced this pull request Sep 25, 2014

@fabpot fabpot minor #12015 [Validator] Simplified testing of violations (webmozart)
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
87123f6

@webmozart webmozart added a commit that referenced this pull request Sep 30, 2014

@webmozart webmozart feature #12021 [Validator] Added error codes to all constraints with …
…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
95d68a1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment