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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function getInvalidValue();
/**
* Returns a machine-digestible error code for the violation.
*
* @return mixed The error code.
* @return string|null The error code.
*/
public function getCode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Used for the comparison of values.
*
* @author Daniel Holmes <daniel@danielholmes.org>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
abstract class AbstractComparison extends Constraint
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ public function validate($value, Constraint $constraint)
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
->setCode($this->getErrorCode())
->addViolation();
} else {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
->setCode($this->getErrorCode())
->addViolation();
}
}
Expand All @@ -80,4 +82,13 @@ public function validate($value, Constraint $constraint)
* @return bool true if the relationship is valid, false otherwise
*/
abstract protected function compareValues($value1, $value2);

/**
* Returns the error code used if the comparison fails.
*
* @return string|null The error code or `null` if no code should be set
*/
protected function getErrorCode()
{
}
}
6 changes: 6 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Blank.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@
*/
class Blank extends Constraint
{
const NOT_BLANK_ERROR = '183ad2de-533d-4796-a439-6d3c3852b549';

protected static $errorNames = array(
self::NOT_BLANK_ERROR => 'NOT_BLANK_ERROR',
);

public $message = 'This value should be blank.';
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ public function validate($value, Constraint $constraint)
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Blank::NOT_BLANK_ERROR)
->addViolation();
} else {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Blank::NOT_BLANK_ERROR)
->addViolation();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Validator/Constraints/CardScheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
*/
class CardScheme extends Constraint
{
const NOT_NUMERIC_ERROR = 1;
const INVALID_FORMAT_ERROR = 2;
const NOT_NUMERIC_ERROR = 'a2ad9231-e827-485f-8a1e-ef4d9a6d5c2e';
const INVALID_FORMAT_ERROR = 'a8faedbf-1c2f-4695-8d22-55783be8efed';

protected static $errorNames = array(
self::NOT_NUMERIC_ERROR => 'NOT_NUMERIC_ERROR',
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Validator/Constraints/Choice.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
*/
class Choice extends Constraint
{
const NO_SUCH_CHOICE_ERROR = 1;
const TOO_FEW_ERROR = 2;
const TOO_MANY_ERROR = 3;
const NO_SUCH_CHOICE_ERROR = '8e179f1b-97aa-4560-a02f-2a8b42e49df7';
const TOO_FEW_ERROR = '11edd7eb-5872-4b6e-9f12-89923999fd0e';
const TOO_MANY_ERROR = '9bd98e49-211c-433f-8630-fd1c2d0f08c3';

protected static $errorNames = array(
self::NO_SUCH_CHOICE_ERROR => 'NO_SUCH_CHOICE_ERROR',
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Validator/Constraints/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*/
class Collection extends Composite
{
const MISSING_FIELD_ERROR = 1;
const NO_SUCH_FIELD_ERROR = 2;
const MISSING_FIELD_ERROR = '2fa2158c-2a7f-484b-98aa-975522539ff8';
const NO_SUCH_FIELD_ERROR = '7703c766-b5d5-4cef-ace7-ae0dd82304e9';

protected static $errorNames = array(
self::MISSING_FIELD_ERROR => 'MISSING_FIELD_ERROR',
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Validator/Constraints/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
*/
class Count extends Constraint
{
const TOO_FEW_ERROR = 1;
const TOO_MANY_ERROR = 2;
const TOO_FEW_ERROR = 'bef8e338-6ae5-4caf-b8e2-50e7b0579e69';
const TOO_MANY_ERROR = '756b1212-697c-468d-a9ad-50dd783bb169';

protected static $errorNames = array(
self::TOO_FEW_ERROR => 'TOO_FEW_ERROR',
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@
*/
class Country extends Constraint
{
const NO_SUCH_COUNTRY_ERROR = '8f900c12-61bd-455d-9398-996cd040f7f0';

protected static $errorNames = array(
self::NO_SUCH_COUNTRY_ERROR => 'NO_SUCH_COUNTRY_ERROR',
);

public $message = 'This value is not a valid country.';
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ public function validate($value, Constraint $constraint)
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Country::NO_SUCH_COUNTRY_ERROR)
->addViolation();
} else {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Country::NO_SUCH_COUNTRY_ERROR)
->addViolation();
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Miha Vrhovnik <miha.vrhovnik@pagein.si>
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class Currency extends Constraint
{
const NO_SUCH_CURRENCY_ERROR = '69945ac1-2db4-405f-bec7-d2772f73df52';

protected static $errorNames = array(
self::NO_SUCH_CURRENCY_ERROR => 'NO_SUCH_CURRENCY_ERROR',
);

public $message = 'This value is not a valid currency.';
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* Validates whether a value is a valid currency.
*
* @author Miha Vrhovnik <miha.vrhovnik@pagein.si>
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
Expand Down Expand Up @@ -50,10 +51,12 @@ public function validate($value, Constraint $constraint)
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Currency::NO_SUCH_CURRENCY_ERROR)
->addViolation();
} else {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Currency::NO_SUCH_CURRENCY_ERROR)
->addViolation();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Validator/Constraints/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*/
class Date extends Constraint
{
const INVALID_FORMAT_ERROR = 1;
const INVALID_DATE_ERROR = 2;
const INVALID_FORMAT_ERROR = '69819696-02ac-4a99-9ff0-14e127c4d1bc';
const INVALID_DATE_ERROR = '3c184ce5-b31d-4de7-8b76-326da7b2be93';

protected static $errorNames = array(
self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR',
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Validator/Constraints/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
*/
class DateTime extends Constraint
{
const INVALID_FORMAT_ERROR = 1;
const INVALID_DATE_ERROR = 2;
const INVALID_TIME_ERROR = 3;
const INVALID_FORMAT_ERROR = '1a9da513-2640-4f84-9b6a-4d99dcddc628';
const INVALID_DATE_ERROR = 'd52afa47-620d-4d99-9f08-f4d85b36e33c';
const INVALID_TIME_ERROR = '5e797c9d-74f7-4098-baa3-94390c447b27';

protected static $errorNames = array(
self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR',
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Validator/Constraints/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
*/
class Email extends Constraint
{
const INVALID_FORMAT_ERROR = 1;
const MX_CHECK_FAILED_ERROR = 2;
const HOST_CHECK_FAILED_ERROR = 3;
const INVALID_FORMAT_ERROR = 'bd79c0ab-ddba-46cc-a703-a7a4b08de310';
const MX_CHECK_FAILED_ERROR = 'bf447c1c-0266-4e10-9c6c-573df282e413';
const HOST_CHECK_FAILED_ERROR = '7da53a8b-56f3-4288-bb3e-ee9ede4ef9a1';

protected static $errorNames = array(
self::INVALID_FORMAT_ERROR => 'STRICT_CHECK_FAILED_ERROR',
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Validator/Constraints/EqualTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Daniel Holmes <daniel@danielholmes.org>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class EqualTo extends AbstractComparison
{
const NOT_EQUAL_ERROR = '478618a7-95ba-473d-9101-cabd45e49115';

protected static $errorNames = array(
self::NOT_EQUAL_ERROR => 'NOT_EQUAL_ERROR',
);

public $message = 'This value should be equal to {{ compared_value }}.';
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Validates values are equal (==).
*
* @author Daniel Holmes <daniel@danielholmes.org>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class EqualToValidator extends AbstractComparisonValidator
{
Expand All @@ -25,4 +26,12 @@ protected function compareValues($value1, $value2)
{
return $value1 == $value2;
}

/**
* {@inheritdoc}
*/
protected function getErrorCode()
{
return EqualTo::NOT_EQUAL_ERROR;
}
}
6 changes: 6 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
*/
class Expression extends Constraint
{
const EXPRESSION_FAILED_ERROR = '6b3befbc-2f01-4ddf-be21-b57898905284';

protected static $errorNames = array(
self::EXPRESSION_FAILED_ERROR => 'EXPRESSION_FAILED_ERROR',
);

public $message = 'This value is not valid.';
public $expression;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ public function validate($value, Constraint $constraint)
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Expression::EXPRESSION_FAILED_ERROR)
->addViolation();
} else {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Expression::EXPRESSION_FAILED_ERROR)
->addViolation();
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Validator/Constraints/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class File extends Constraint
{
// Check the Image constraint for clashes if adding new constants here

const NOT_FOUND_ERROR = 1;
const NOT_READABLE_ERROR = 2;
const EMPTY_ERROR = 3;
const TOO_LARGE_ERROR = 4;
const INVALID_MIME_TYPE_ERROR = 5;
const NOT_FOUND_ERROR = 'd2a3fb6e-7ddc-4210-8fbf-2ab345ce1998';
const NOT_READABLE_ERROR = 'c20c92a4-5bfa-4202-9477-28e800e0f6ff';
const EMPTY_ERROR = '5d743385-9775-4aa5-8ff5-495fb1e60137';
const TOO_LARGE_ERROR = 'df8637af-d466-48c6-a59d-e7126250a654';
const INVALID_MIME_TYPE_ERROR = '744f00bc-4389-4c74-92de-9a43cde55534';

protected static $errorNames = array(
self::NOT_FOUND_ERROR => 'NOT_FOUND_ERROR',
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Validator/Constraints/GreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Daniel Holmes <daniel@danielholmes.org>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class GreaterThan extends AbstractComparison
{
const TOO_LOW_ERROR = '778b7ae0-84d3-481a-9dec-35fdb64b1d78';

protected static $errorNames = array(
self::TOO_LOW_ERROR => 'TOO_LOW_ERROR',
);

public $message = 'This value should be greater than {{ compared_value }}.';
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Daniel Holmes <daniel@danielholmes.org>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class GreaterThanOrEqual extends AbstractComparison
{
const TOO_LOW_ERROR = 'ea4e51d1-3342-48bd-87f1-9e672cd90cad';

protected static $errorNames = array(
self::TOO_LOW_ERROR => 'TOO_LOW_ERROR',
);

public $message = 'This value should be greater than or equal to {{ compared_value }}.';
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Validates values are greater than or equal to the previous (>=).
*
* @author Daniel Holmes <daniel@danielholmes.org>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class GreaterThanOrEqualValidator extends AbstractComparisonValidator
{
Expand All @@ -25,4 +26,12 @@ protected function compareValues($value1, $value2)
{
return $value1 >= $value2;
}

/**
* {@inheritdoc}
*/
protected function getErrorCode()
{
return GreaterThanOrEqual::TOO_LOW_ERROR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Validates values are greater than the previous (>).
*
* @author Daniel Holmes <daniel@danielholmes.org>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class GreaterThanValidator extends AbstractComparisonValidator
{
Expand All @@ -25,4 +26,12 @@ protected function compareValues($value1, $value2)
{
return $value1 > $value2;
}

/**
* {@inheritdoc}
*/
protected function getErrorCode()
{
return GreaterThan::TOO_LOW_ERROR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*
* @Annotation
* @Target({"CLASS", "ANNOTATION"})
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class GroupSequenceProvider
{
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Validator/Constraints/Iban.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
*/
class Iban extends Constraint
{
const TOO_SHORT_ERROR = 1;
const INVALID_COUNTRY_CODE_ERROR = 2;
const INVALID_CHARACTERS_ERROR = 3;
const INVALID_CASE_ERROR = 4;
const CHECKSUM_FAILED_ERROR = 5;
const TOO_SHORT_ERROR = '88e5e319-0aeb-4979-a27e-3d9ce0c16166';
const INVALID_COUNTRY_CODE_ERROR = 'de78ee2c-bd50-44e2-aec8-3d8228aeadb9';
const INVALID_CHARACTERS_ERROR = '8d3d85e4-784f-4719-a5bc-d9e40d45a3a5';
const INVALID_CASE_ERROR = 'f4bf62fe-03ec-42af-a53b-68e21b1e7274';
const CHECKSUM_FAILED_ERROR = 'b9401321-f9bf-4dcb-83c1-f31094440795';

protected static $errorNames = array(
self::TOO_SHORT_ERROR => 'TOO_SHORT_ERROR',
Expand Down
Loading