Skip to content
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

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

Closed
wants to merge 7 commits into from
Closed

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

wants to merge 7 commits into from

Conversation

aeoris
Copy link
Contributor

@aeoris 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);
Copy link
Contributor

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 !

Copy link
Contributor Author

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?

Copy link
Contributor

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.

@aeoris
Copy link
Contributor Author

aeoris commented Mar 12, 2013

Ping @vicb

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

@vicb
Copy link
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
Copy link
Contributor Author

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.

@stof
Copy link
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

@fabpot
Copy link
Member

fabpot commented Sep 13, 2013

ping @bschussek

@webmozart
Copy link
Contributor

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

@aeoris Do you have time to update this PR?

@aeoris
Copy link
Contributor Author

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!

…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
@aeoris
Copy link
Contributor Author

aeoris commented Nov 8, 2013

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

@cordoval
Copy link
Contributor

cordoval commented Dec 6, 2013

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

@aeoris
Copy link
Contributor Author

aeoris commented Dec 6, 2013

@cordoval done, thanks!

@cordoval
Copy link
Contributor

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

@aeoris
Copy link
Contributor Author

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)

@aeoris
Copy link
Contributor Author

aeoris commented Feb 15, 2014

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

@stloyd
Copy link
Contributor

stloyd commented Mar 11, 2014

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

@aeoris
Copy link
Contributor Author

aeoris commented May 23, 2014

@webmozart any progress on this?

webmozart added a commit that referenced this pull request Aug 19, 2014
… (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()
@webmozart
Copy link
Contributor

Replaced by #12005.

@webmozart webmozart closed this Sep 23, 2014
fabpot added a commit that referenced this pull request Sep 25, 2014
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
webmozart added a commit that referenced this pull request Sep 30, 2014
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants