Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 52cb7df

Browse filesBrowse files
committed
[Validator] Added error codes to all constraints with multiple error causes
1 parent 2848f63 commit 52cb7df
Copy full SHA for 52cb7df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree

52 files changed

+685
-288
lines changed

‎src/Symfony/Component/Validator/Constraint.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraint.php
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator;
1313

1414
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
15+
use Symfony\Component\Validator\Exception\InvalidArgumentException;
1516
use Symfony\Component\Validator\Exception\InvalidOptionsException;
1617
use Symfony\Component\Validator\Exception\MissingOptionsException;
1718

@@ -50,6 +51,34 @@ abstract class Constraint
5051
*/
5152
const PROPERTY_CONSTRAINT = 'property';
5253

54+
/**
55+
* Maps error codes to the names of their constants
56+
* @var array
57+
*/
58+
protected static $errorNames = array();
59+
60+
/**
61+
* Returns the name of the given error code.
62+
*
63+
* @param int $errorCode The error code
64+
*
65+
* @return string The name of the error code
66+
*
67+
* @throws InvalidArgumentException If the error code does not exist
68+
*/
69+
public static function getErrorName($errorCode)
70+
{
71+
if (!isset(static::$errorNames[$errorCode])) {
72+
throw new InvalidArgumentException(sprintf(
73+
'The error code "%s" does not exist for constraint of type "%s".',
74+
$errorCode,
75+
get_called_class()
76+
));
77+
}
78+
79+
return static::$errorNames[$errorCode];
80+
}
81+
5382
/**
5483
* Initializes the constraint with options.
5584
*

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/CardScheme.php
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,18 @@
2020
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
2121
*
2222
* @author Tim Nagel <t.nagel@infinite.net.au>
23+
* @author Bernhard Schussek <bschussek@gmail.com>
2324
*/
2425
class CardScheme extends Constraint
2526
{
27+
const ERROR_NOT_NUMERIC = 1;
28+
const ERROR_INVALID_FORMAT = 2;
29+
30+
protected static $errorNames = array(
31+
self::ERROR_NOT_NUMERIC => 'ERROR_NOT_NUMERIC',
32+
self::ERROR_INVALID_FORMAT => 'ERROR_INVALID_FORMAT',
33+
);
34+
2635
public $message = 'Unsupported card type or invalid card number.';
2736
public $schemes;
2837

‎src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public function validate($value, Constraint $constraint)
117117
if (!is_numeric($value)) {
118118
$this->buildViolation($constraint->message)
119119
->setParameter('{{ value }}', $this->formatValue($value))
120+
->setCode(CardScheme::ERROR_NOT_NUMERIC)
120121
->addViolation();
121122

122123
return;
@@ -135,6 +136,7 @@ public function validate($value, Constraint $constraint)
135136

136137
$this->buildViolation($constraint->message)
137138
->setParameter('{{ value }}', $this->formatValue($value))
139+
->setCode(CardScheme::ERROR_INVALID_FORMAT)
138140
->addViolation();
139141
}
140142
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/Choice.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
*/
2424
class Choice extends Constraint
2525
{
26+
const ERROR_NO_SUCH_CHOICE = 1;
27+
const ERROR_TOO_FEW = 2;
28+
const ERROR_TOO_MANY = 3;
29+
30+
protected static $errorNames = array(
31+
self::ERROR_NO_SUCH_CHOICE => 'ERROR_NO_SUCH_CHOICE',
32+
self::ERROR_TOO_FEW => 'ERROR_TOO_FEW',
33+
self::ERROR_TOO_MANY => 'ERROR_TOO_MANY',
34+
);
35+
2636
public $choices;
2737
public $callback;
2838
public $multiple = false;

‎src/Symfony/Component/Validator/Constraints/ChoiceValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/ChoiceValidator.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function validate($value, Constraint $constraint)
6565
if (!in_array($_value, $choices, $constraint->strict)) {
6666
$this->buildViolation($constraint->multipleMessage)
6767
->setParameter('{{ value }}', $this->formatValue($_value))
68+
->setCode(Choice::ERROR_NO_SUCH_CHOICE)
6869
->setInvalidValue($_value)
6970
->addViolation();
7071

@@ -78,6 +79,7 @@ public function validate($value, Constraint $constraint)
7879
$this->buildViolation($constraint->minMessage)
7980
->setParameter('{{ limit }}', $constraint->min)
8081
->setPlural((int) $constraint->min)
82+
->setCode(Choice::ERROR_TOO_FEW)
8183
->addViolation();
8284

8385
return;
@@ -87,13 +89,15 @@ public function validate($value, Constraint $constraint)
8789
$this->buildViolation($constraint->maxMessage)
8890
->setParameter('{{ limit }}', $constraint->max)
8991
->setPlural((int) $constraint->max)
92+
->setCode(Choice::ERROR_TOO_MANY)
9093
->addViolation();
9194

9295
return;
9396
}
9497
} elseif (!in_array($value, $choices, $constraint->strict)) {
9598
$this->buildViolation($constraint->message)
9699
->setParameter('{{ value }}', $this->formatValue($value))
100+
->setCode(Choice::ERROR_NO_SUCH_CHOICE)
97101
->addViolation();
98102
}
99103
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/Collection.php
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Validator\Constraint;
1514
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1615

1716
/**
@@ -24,6 +23,14 @@
2423
*/
2524
class Collection extends Composite
2625
{
26+
const ERROR_MISSING_FIELD = 1;
27+
const ERROR_NO_SUCH_FIELD = 2;
28+
29+
protected static $errorNames = array(
30+
self::ERROR_MISSING_FIELD => 'ERROR_MISSING_FIELD',
31+
self::ERROR_NO_SUCH_FIELD => 'ERROR_NO_SUCH_FIELD',
32+
);
33+
2734
public $fields = array();
2835
public $allowExtraFields = false;
2936
public $allowMissingFields = false;

‎src/Symfony/Component/Validator/Constraints/CollectionValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/CollectionValidator.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function validate($value, Constraint $constraint)
7373
->atPath('['.$field.']')
7474
->setParameter('{{ field }}', $this->formatValue($field))
7575
->setInvalidValue(null)
76+
->setCode(Collection::ERROR_MISSING_FIELD)
7677
->addViolation();
7778
}
7879
}
@@ -84,6 +85,7 @@ public function validate($value, Constraint $constraint)
8485
->atPath('['.$field.']')
8586
->setParameter('{{ field }}', $this->formatValue($field))
8687
->setInvalidValue($fieldValue)
88+
->setCode(Collection::ERROR_NO_SUCH_FIELD)
8789
->addViolation();
8890
}
8991
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/Count.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
*/
2525
class Count extends Constraint
2626
{
27+
const ERROR_TOO_FEW = 1;
28+
const ERROR_TOO_MANY = 2;
29+
30+
protected static $errorNames = array(
31+
self::ERROR_TOO_FEW => 'ERROR_TOO_FEW',
32+
self::ERROR_TOO_MANY => 'ERROR_TOO_MANY',
33+
);
34+
2735
public $minMessage = 'This collection should contain {{ limit }} element or more.|This collection should contain {{ limit }} elements or more.';
2836
public $maxMessage = 'This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less.';
2937
public $exactMessage = 'This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements.';

‎src/Symfony/Component/Validator/Constraints/CountValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/CountValidator.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function validate($value, Constraint $constraint)
4141
->setParameter('{{ limit }}', $constraint->max)
4242
->setInvalidValue($value)
4343
->setPlural((int) $constraint->max)
44+
->setCode(Count::ERROR_TOO_MANY)
4445
->addViolation();
4546

4647
return;
@@ -52,6 +53,7 @@ public function validate($value, Constraint $constraint)
5253
->setParameter('{{ limit }}', $constraint->min)
5354
->setInvalidValue($value)
5455
->setPlural((int) $constraint->min)
56+
->setCode(Count::ERROR_TOO_FEW)
5557
->addViolation();
5658
}
5759
}

‎src/Symfony/Component/Validator/Constraints/Date.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/Date.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,13 @@
2323
*/
2424
class Date extends Constraint
2525
{
26+
const ERROR_INVALID_FORMAT = 1;
27+
const ERROR_INVALID_DATE = 2;
28+
29+
protected static $errorNames = array(
30+
self::ERROR_INVALID_FORMAT => 'ERROR_INVALID_FORMAT',
31+
self::ERROR_INVALID_DATE => 'ERROR_INVALID_DATE',
32+
);
33+
2634
public $message = 'This value is not a valid date.';
2735
}

‎src/Symfony/Component/Validator/Constraints/DateTime.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/DateTime.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,15 @@
2323
*/
2424
class DateTime extends Constraint
2525
{
26+
const ERROR_INVALID_FORMAT = 1;
27+
const ERROR_INVALID_DATE = 2;
28+
const ERROR_INVALID_TIME = 3;
29+
30+
protected static $errorNames = array(
31+
self::ERROR_INVALID_FORMAT => 'ERROR_INVALID_FORMAT',
32+
self::ERROR_INVALID_DATE => 'ERROR_INVALID_DATE',
33+
self::ERROR_INVALID_TIME => 'ERROR_INVALID_TIME',
34+
);
35+
2636
public $message = 'This value is not a valid datetime.';
2737
}

‎src/Symfony/Component/Validator/Constraints/DateTimeValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/DateTimeValidator.php
+11-2Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
class DateTimeValidator extends DateValidator
2323
{
24-
const PATTERN = '/^(\d{4})-(\d{2})-(\d{2}) (0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/';
24+
const PATTERN = '/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/';
2525

2626
/**
2727
* {@inheritdoc}
@@ -45,14 +45,23 @@ public function validate($value, Constraint $constraint)
4545
if (!preg_match(static::PATTERN, $value, $matches)) {
4646
$this->buildViolation($constraint->message)
4747
->setParameter('{{ value }}', $this->formatValue($value))
48+
->setCode(DateTime::ERROR_INVALID_FORMAT)
4849
->addViolation();
4950

5051
return;
5152
}
5253

53-
if (!checkdate($matches[2], $matches[3], $matches[1])) {
54+
if (!DateValidator::checkDate($matches[1], $matches[2], $matches[3])) {
5455
$this->buildViolation($constraint->message)
5556
->setParameter('{{ value }}', $this->formatValue($value))
57+
->setCode(DateTime::ERROR_INVALID_DATE)
58+
->addViolation();
59+
}
60+
61+
if (!TimeValidator::checkTime($matches[4], $matches[5], $matches[6])) {
62+
$this->buildViolation($constraint->message)
63+
->setParameter('{{ value }}', $this->formatValue($value))
64+
->setCode(DateTime::ERROR_INVALID_TIME)
5665
->addViolation();
5766
}
5867
}

‎src/Symfony/Component/Validator/Constraints/DateValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/DateValidator.php
+19-1Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ class DateValidator extends ConstraintValidator
2424
{
2525
const PATTERN = '/^(\d{4})-(\d{2})-(\d{2})$/';
2626

27+
/**
28+
* Checks whether a date is valid.
29+
*
30+
* @param int $year The year
31+
* @param int $month The month
32+
* @param int $day The day
33+
*
34+
* @return bool Whether the date is valid
35+
*
36+
* @internal
37+
*/
38+
public static function checkDate($year, $month, $day)
39+
{
40+
return checkdate($month, $day, $year);
41+
}
42+
2743
/**
2844
* {@inheritdoc}
2945
*/
@@ -46,14 +62,16 @@ public function validate($value, Constraint $constraint)
4662
if (!preg_match(static::PATTERN, $value, $matches)) {
4763
$this->buildViolation($constraint->message)
4864
->setParameter('{{ value }}', $this->formatValue($value))
65+
->setCode(Date::ERROR_INVALID_FORMAT)
4966
->addViolation();
5067

5168
return;
5269
}
5370

54-
if (!checkdate($matches[2], $matches[3], $matches[1])) {
71+
if (!self::checkDate($matches[1], $matches[2], $matches[3])) {
5572
$this->buildViolation($constraint->message)
5673
->setParameter('{{ value }}', $this->formatValue($value))
74+
->setCode(Date::ERROR_INVALID_DATE)
5775
->addViolation();
5876
}
5977
}

‎src/Symfony/Component/Validator/Constraints/Email.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/Email.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
*/
2424
class Email extends Constraint
2525
{
26+
const ERROR_INVALID_FORMAT = 1;
27+
const ERROR_MX_CHECK_FAILED = 2;
28+
const ERROR_HOST_CHECK_FAILED = 3;
29+
30+
protected static $errorNames = array(
31+
self::ERROR_INVALID_FORMAT => 'ERROR_STRICT_CHECK_FAILED',
32+
self::ERROR_MX_CHECK_FAILED => 'ERROR_MX_CHECK_FAILED',
33+
self::ERROR_HOST_CHECK_FAILED => 'ERROR_HOST_CHECK_FAILED',
34+
);
35+
2636
public $message = 'This value is not a valid email address.';
2737
public $checkMX = false;
2838
public $checkHost = false;

‎src/Symfony/Component/Validator/Constraints/EmailValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/EmailValidator.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ public function validate($value, Constraint $constraint)
6868
if (!$strictValidator->isValid($value, false, true)) {
6969
$this->buildViolation($constraint->message)
7070
->setParameter('{{ value }}', $this->formatValue($value))
71+
->setCode(Email::ERROR_INVALID_FORMAT)
7172
->addViolation();
7273

7374
return;
7475
}
7576
} elseif (!preg_match('/.+\@.+\..+/', $value)) {
7677
$this->buildViolation($constraint->message)
7778
->setParameter('{{ value }}', $this->formatValue($value))
79+
->setCode(Email::ERROR_INVALID_FORMAT)
7880
->addViolation();
7981

8082
return;
@@ -87,6 +89,7 @@ public function validate($value, Constraint $constraint)
8789
if (!$this->checkMX($host)) {
8890
$this->buildViolation($constraint->message)
8991
->setParameter('{{ value }}', $this->formatValue($value))
92+
->setCode(Email::ERROR_MX_CHECK_FAILED)
9093
->addViolation();
9194
}
9295

@@ -97,6 +100,7 @@ public function validate($value, Constraint $constraint)
97100
if (!$this->checkHost($host)) {
98101
$this->buildViolation($constraint->message)
99102
->setParameter('{{ value }}', $this->formatValue($value))
103+
->setCode(Email::ERROR_HOST_CHECK_FAILED)
100104
->addViolation();
101105
}
102106
}

‎src/Symfony/Component/Validator/Constraints/File.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/File.php
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@
2424
*/
2525
class File extends Constraint
2626
{
27+
const ERROR_FILE_NOT_FOUND = 1;
28+
const ERROR_FILE_NOT_READABLE = 2;
29+
const ERROR_FILE_EMPTY = 3;
30+
const ERROR_TOO_LARGE = 4;
31+
const ERROR_INVALID_MIME_TYPE = 5;
32+
33+
protected static $errorNames = array(
34+
self::ERROR_FILE_NOT_FOUND => 'ERROR_FILE_NOT_FOUND',
35+
self::ERROR_FILE_NOT_READABLE => 'ERROR_FILE_NOT_READABLE',
36+
self::ERROR_FILE_EMPTY => 'ERROR_FILE_EMPTY',
37+
self::ERROR_TOO_LARGE => 'ERROR_TOO_LARGE',
38+
self::ERROR_INVALID_MIME_TYPE => 'ERROR_INVALID_MIME_TYPE',
39+
);
40+
2741
public $maxSize;
2842
public $binaryFormat;
2943
public $mimeTypes = array();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.