10BC0 feature #15154 [Validator] Added missing error codes and turned codes… · symfony/symfony@bfa28d6 · GitHub
[go: up one dir, main page]

Skip to content

Commit bfa28d6

Browse files
committed
feature #15154 [Validator] Added missing error codes and turned codes 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
2 parents 32cbfd4 + 8874e88 commit bfa28d6

File tree

97 files changed

+480
-100
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+480
-100
lines changed

src/Symfony/Component/Validator/ConstraintViolationInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function getInvalidValue();
130130
/**
131131
* Returns a machine-digestible error code for the violation.
132132
*
133-
* @return mixed The error code.
133+
* @return string|null The error code.
134134
*/
135135
public function getCode();
136136
}

src/Symfony/Component/Validator/Constraints/AbstractComparison.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Used for the comparison of values.
1919
*
2020
* @author Daniel Holmes <daniel@danielholmes.org>
21+
* @author Bernhard Schussek <bschussek@gmail.com>
2122
*/
2223
abstract class AbstractComparison extends Constraint
2324
{

src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ public function validate($value, Constraint $constraint)
6060
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
6161
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
6262
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
63+
->setCode($this->getErrorCode())
6364
->addViolation();
6465
} else {
6566
$this->buildViolation($constraint->message)
6667
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
6768
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
6869
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
70+
->setCode($this->getErrorCode())
6971
->addViolation();
7072
}
7173
}
@@ -80,4 +82,13 @@ public function validate($value, Constraint $constraint)
8082
* @return bool true if the relationship is valid, false otherwise
8183
*/
8284
abstract protected function compareValues($value1, $value2);
85+
86+
/**
87+
* Returns the error code used if the comparison fails.
88+
*
89+
* @return string|null The error code or `null` if no code should be set
90+
*/
91+
protected function getErrorCode()
92+
{
93+
}
8394
}

src/Symfony/Component/Validator/Constraints/Blank.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,11 @@
2323
*/
2424
class Blank extends Constraint
2525
{
26+
const NOT_BLANK_ERROR = '183ad2de-533d-4796-a439-6d3c3852b549';
27+
28+
protected static $errorNames = array(
29+
self::NOT_BLANK_ERROR => 'NOT_BLANK_ERROR',
30+
);
31+
2632
public $message = 'This value should be blank.';
2733
}

src/Symfony/Component/Validator/Constraints/BlankValidator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ public function validate($value, Constraint $constraint)
3636
if ($this->context instanceof ExecutionContextInterface) {
3737
$this->context->buildViolation($constraint->message)
3838
->setParameter('{{ value }}', $this->formatValue($value))
39+
->setCode(Blank::NOT_BLANK_ERROR)
3940
->addViolation();
4041
} else {
4142
$this->buildViolation($constraint->message)
4243
->setParameter('{{ value }}', $this->formatValue($value))
44+
->setCode(Blank::NOT_BLANK_ERROR)
4345
->addViolation();
4446
}
4547
}

src/Symfony/Component/Validator/Constraints/CardScheme.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
*/
2525
class CardScheme extends Constraint
2626
{
27-
const NOT_NUMERIC_ERROR = 1;
28-
const INVALID_FORMAT_ERROR = 2;
27+
const NOT_NUMERIC_ERROR = 'a2ad9231-e827-485f-8a1e-ef4d9a6d5c2e';
28+
const INVALID_FORMAT_ERROR = 'a8faedbf-1c2f-4695-8d22-55783be8efed';
2929

3030
protected static $errorNames = array(
3131
self::NOT_NUMERIC_ERROR => 'NOT_NUMERIC_ERROR',

src/Symfony/Component/Validator/Constraints/Choice.php

Lines changed: 3 additions & 3 deletions
26
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
*/
2424
class Choice extends Constraint
2525
{
26-
const NO_SUCH_CHOICE_ERROR = 1;
27-
const TOO_FEW_ERROR = 2;
28-
const TOO_MANY_ERROR = 3;
+
const NO_SUCH_CHOICE_ERROR = '8e179f1b-97aa-4560-a02f-2a8b42e49df7';
27+
const TOO_FEW_ERROR = '11edd7eb-5872-4b6e-9f12-89923999fd0e';
28+
const TOO_MANY_ERROR = '9bd98e49-211c-433f-8630-fd1c2d0f08c3';
2929

3030
protected static $errorNames = array(
3131
self::NO_SUCH_CHOICE_ERROR => 'NO_SUCH_CHOICE_ERROR',

src/Symfony/Component/Validator/Constraints/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
*/
2424
class Collection extends Composite
2525
{
26-
const MISSING_FIELD_ERROR = 1;
27-
const NO_SUCH_FIELD_ERROR = 2;
26+
const MISSING_FIELD_ERROR = '2fa2158c-2a7f-484b-98aa-975522539ff8';
27+
const NO_SUCH_FIELD_ERROR = '7703c766-b5d5-4cef-ace7-ae0dd82304e9';
2828

2929
protected static $errorNames = array(
3030
self::MISSING_FIELD_ERROR => 'MISSING_FIELD_ERROR',

src/Symfony/Component/Validator/Constraints/Count.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
*/
2525
class Count extends Constraint
2626
{
27-
const TOO_FEW_ERROR = 1;
28-
const TOO_MANY_ERROR = 2;
27+
const TOO_FEW_ERROR = 'bef8e338-6ae5-4caf-b8e2-50e7b0579e69';
28+
const TOO_MANY_ERROR = '756b1212-697c-468d-a9ad-50dd783bb169';
2929

3030
protected static $errorNames = array(
3131
self::TOO_FEW_ERROR => 'TOO_FEW_ERROR',

src/Symfony/Component/Validator/Constraints/Country.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,11 @@
2323
*/
2424
class Country extends Constraint
2525
{
26+
const NO_SUCH_COUNTRY_ERROR = '8f900c12-61bd-455d-9398-996cd040f7f0';
27+
28+
protected static $errorNames = array(
29+
self::NO_SUCH_COUNTRY_ERROR => 'NO_SUCH_COUNTRY_ERROR',
30+
);
31+
2632
public $message = 'This value is not a valid country.';
2733
}

0 commit comments

Comments
 (0)
0