Skip to content

[Validator] Added missing error codes and turned codes into UUIDs #15154

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

Merged
merged 1 commit into from
Jul 1, 2015

Conversation

webmozart
Copy link
Contributor

Reopened #12388 on the 2.8 branch.

Q A
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

With the current implementation of error codes, checking which error occurred is unnecessarily complex:

if ($violation->getConstraint() instanceof Length && Length::TOO_SHORT_ERROR === $violation->getCode()) {
    // ...
}

Also, the code is completely missing for some constraints. This is fixed now. By using UUIDs, the check is reduced to:

if (Length::TOO_SHORT_ERROR === $violation->getCode()) {
    // ...
}

Also, APIs can simply output the error code and the name of the error without needing to point to the constraint as well.

Before:

[
    {
        "code": "1",
        "name": "TOO_SHORT_ERROR",
        "message": "This value is too short. ...",
        "constraint": "Symfony\\Component\\Validator\\Constraints\\Length"
    }
]

After:

[
    {
        "code": "9ff3fdc4-b214-49db-8718-39c315e33d45",
        "name": "TOO_SHORT_ERROR",
        "message": "This value is too short. ..."
    }
]

This makes it possible to implement a service on symfony.com which looks up error codes, e.g.

symfony.com/error?code=9ff3fdc4-b214-49db-8718-39c315e33d45

Such a URL could redirect directly to the documentation of the appropriate constraint. We could even support user-submitted error codes which redirect to the documentation of that constraint.

@stof
Copy link
Member

stof commented Jul 1, 2015

👍

@fabpot
Copy link
Member

fabpot commented Jul 1, 2015

Thank you @webmozart.

@fabpot fabpot merged commit 8874e88 into symfony:2.8 Jul 1, 2015
fabpot added a commit that referenced this pull request Jul 1, 2015
… into UUIDs (webmozart)

This PR was merged into the 2.8 branch.

Discussion
----------

[Validator] Added missing error codes and turned codes into UUIDs

Reopened #12388 on the 2.8 branch.

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

With the current implementation of error codes, checking which error occurred is unnecessarily complex:

```php
if ($violation->getConstraint() instanceof Length && Length::TOO_SHORT_ERROR === $violation->getCode()) {
    // ...
}
```

Also, the code is completely missing for some constraints. This is fixed now. By using UUIDs, the check is reduced to:

```php
if (Length::TOO_SHORT_ERROR === $violation->getCode()) {
    // ...
}
```

Also, APIs can simply output the error code and the name of the error without needing to point to the constraint as well.

Before:

```json
[
    {
        "code": "1",
        "name": "TOO_SHORT_ERROR",
        "message": "This value is too short. ...",
        "constraint": "Symfony\\Component\\Validator\\Constraints\\Length"
    }
]
```

After:

```json
[
    {
        "code": "9ff3fdc4-b214-49db-8718-39c315e33d45",
        "name": "TOO_SHORT_ERROR",
        "message": "This value is too short. ..."
    }
]
```

This makes it possible to implement a service on symfony.com which looks up error codes, e.g.

symfony.com/error?code=9ff3fdc4-b214-49db-8718-39c315e33d45

Such a URL could redirect directly to the documentation of the appropriate constraint. We could even support user-submitted error codes which redirect to the documentation of that constraint.

Commits
-------

8874e88 [Validator] Added missing error codes and turned codes into UUIDs
@fabpot fabpot mentioned this pull request Nov 16, 2015
fabpot added a commit that referenced this pull request Jul 8, 2016
…(Koc)

This PR was merged into the 2.8 branch.

Discussion
----------

[DoctrineBridge] added missing error code for constraint.

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | wait for travis
| Fixed tickets | #15154
| License       | MIT
| Doc PR        | -

Commits
-------

32cb269 [DoctrineBridge] added missing error code for constraint.
fabpot added a commit that referenced this pull request Mar 22, 2017
…straint fails. (Koc)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[DX][Form][Validator] Add ability check if cocrete constraint fails.

| Q | A |
| --- | --- |
| Branch? | master |
| Bug fix? | no |
| New feature? | yes |
| BC breaks? | no |
| Deprecations? | no |
| Tests pass? | wait for travis |
| Fixed tickets | #15154 |
| License | MIT |
| Doc PR | should open |

Sometimes for big forms with multiple constraints we should handle some errors separately.

``` php
// when using validator
$constraintViolations = $validator->validate(...);
if (count($constraintViolations->findByCodes(UniqueEntity::NOT_UNIQUE_ERROR))) {
  // display some message or send email or etc
}

// when using forms
if (count($form->getErrors()->findByCodes(UniqueEntity::NOT_UNIQUE_ERROR))) {
  // display some message or send email or etc
}
```

This PR add some useful methods to handle this. Before we should iterate all failed constraints using foreach.

Feel free to suggest better names for new methods.

Commits
-------

29a3a7e Add ability retrieve errors by their code.
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.

3 participants