Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cb35a25

Browse files
committed
[Validator] Added error codes to all constraints with multiple error causes
1 parent 2848f63 commit cb35a25

Some content is hidden

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

58 files changed

+733
-343
lines changed

src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,9 @@ public function validate($entity, Constraint $constraint)
137137

138138
$errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];
139139

140-
if ($this->context instanceof ExecutionContextInterface) {
141-
$this->context->buildViolation($constraint->message)
142-
->atPath($errorPath)
143-
->setInvalidValue($criteria[$fields[0]])
144-
->addViolation();
145-
} else {
146-
// 2.4 API
147-
$this->context->addViolationAt($errorPath, $constraint->message, array(), $criteria[$fields[0]]);
148-
}
140+
$this->buildViolation($constraint->message)
141+
->atPath($errorPath)
142+
->setInvalidValue($criteria[$fields[0]])
143+
->addViolation();
149144
}
150145
}

src/Symfony/Component/Form/Extension/Validator/Constraints/Form.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,20 @@
1818
*/
1919
class Form extends Constraint
2020
{
21+
const ERROR_NOT_SYNCHRONIZED = 1;
22+
const ERROR_NO_SUCH_FIELD = 2;
23+
2124
/**
22-
* Violation code marking an invalid form.
25+
* @deprecated Deprecated since Symfony 2.6, to be removed in 3.0. Use
26+
* {@self ERROR_NOT_SYNCHRONIZED} instead.
2327
*/
2428
const ERR_INVALID = 1;
2529

30+
protected static $errorNames = array(
31+
self::ERROR_NOT_SYNCHRONIZED => 'ERROR_NOT_SYNCHRONIZED',
32+
self::ERROR_NO_SUCH_FIELD => 'ERROR_NO_SUCH_FIELD',
33+
);
34+
2635
/**
2736
* {@inheritdoc}
2837
*/

src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -116,40 +116,21 @@ public function validate($form, Constraint $constraint)
116116
? (string) $form->getViewData()
117117
: gettype($form->getViewData());
118118

119-
if ($this->context instanceof ExecutionContextInterface) {
120-
$this->context->buildViolation($config->getOption('invalid_message'))
121-
->setParameters(array_replace(array('{{ value }}' => $clientDataAsString), $config->getOption('invalid_message_parameters')))
122-
->setInvalidValue($form->getViewData())
123-
->setCode(Form::ERR_INVALID)
124-
->addViolation();
125-
} else {
126-
// 2.4 API
127-
$this->context->addViolation(
128-
$config->getOption('invalid_message'),
129-
array_replace(array('{{ value }}' => $clientDataAsString), $config->getOption('invalid_message_parameters')),
130-
$form->getViewData(),
131-
null,
132-
Form::ERR_INVALID
133-
);
134-
}
119+
$this->buildViolation($config->getOption('invalid_message'))
120+
->setParameters(array_replace(array('{{ value }}' => $clientDataAsString), $config->getOption('invalid_message_parameters')))
121+
->setInvalidValue($form->getViewData())
122+
->setCode(Form::ERROR_NOT_SYNCHRONIZED)
123+
->addViolation();
135124
}
136125
}
137126

138127
// Mark the form with an error if it contains extra fields
139128
if (!$config->getOption('allow_extra_fields') && count($form->getExtraData()) > 0) {
140-
if ($this->context instanceof ExecutionContextInterface) {
141-
$this->context->buildViolation($config->getOption('extra_fields_message'))
142-
->setParameter('{{ extra_fields }}', implode('", "', array_keys($form->getExtraData())))
143-
->setInvalidValue($form->getExtraData())
144-
->addViolation();
145-
} else {
146-
// 2.4 API
147-
$this->context->addViolation(
148-
$config->getOption('extra_fields_message'),
149-
array('{{ extra_fields }}' => implode('", "', array_keys($form->getExtraData()))),
150-
$form->getExtraData()
151-
);
152-
}
129+
$this->buildViolation($config->getOption('extra_fields_message'))
130+
->setParameter('{{ extra_fields }}', implode('", "', array_keys($form->getExtraData())))
131+
->setInvalidValue($form->getExtraData())
132+
->setCode(Form::ERROR_NO_SUCH_FIELD)
133+
->addViolation();
153134
}
154135

155136
// Mark the form with an error if the uploaded size was too large

src/Symfony/Component/Form/Extension/Validator/EventListener/ValidationListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function validateForm(FormEvent $event)
6666
foreach ($violations as $violation) {
6767
// Allow the "invalid" constraint to be put onto
6868
// non-synchronized forms
69-
$allowNonSynchronized = Form::ERR_INVALID === $violation->getCode();
69+
$allowNonSynchronized = Form::ERROR_NOT_SYNCHRONIZED === $violation->getCode();
7070

7171
$this->violationMapper->mapViolation($violation, $form, $allowNonSynchronized);
7272
}

src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,12 @@ function () { throw new TransformationFailedException(); }
225225

226226
$this->validator->validate($form, new Form());
227227

228-
$this->assertViolation('invalid_message_key', array(
229-
'{{ value }}' => 'foo',
230-
'{{ foo }}' => 'bar',
231-
), 'property.path', 'foo', null, Form::ERR_INVALID);
228+
$this->assertViolation('invalid_message_key')
229+
->withParameter('{{ value }}', 'foo')
230+
->withParameter('{{ foo }}', 'bar')
231+
->withInvalidValue('foo')
232+
->withCode(Form::ERROR_NOT_SYNCHRONIZED)
233+
->assertNow();
232234
}
233235

234236
public function testAddInvalidErrorEvenIfNoValidationGroups()
@@ -257,10 +259,12 @@ function () { throw new TransformationFailedException(); }
257259

258260
$this->validator->validate($form, new Form());
259261

260-
$this->assertViolation('invalid_message_key', array(
261-
'{{ value }}' => 'foo',
262-
'{{ foo }}' => 'bar',
263-
), 'property.path', 'foo', null, Form::ERR_INVALID);
262+
$this->assertViolation('invalid_message_key')
263+
->withParameter('{{ value }}', 'foo')
264+
->withParameter('{{ foo }}', 'bar')
265+
->withInvalidValue('foo')
266+
->withCode(Form::ERROR_NOT_SYNCHRONIZED)
267+
->assertNow();
264268
}
265269

266270
public function testDontValidateConstraintsIfNotSynchronized()
@@ -289,9 +293,11 @@ function () { throw new TransformationFailedException(); }
289293

290294
$this->validator->validate($form, new Form());
291295

292-
$this->assertViolation('invalid_message_key', array(
293-
'{{ value }}' => 'foo',
294-
), 'property.path','foo', null, Form::ERR_INVALID);
296+
$this->assertViolation('invalid_message_key')
297+
->withParameter('{{ value }}', 'foo')
298+
->withInvalidValue('foo')
299+
->withCode(Form::ERROR_NOT_SYNCHRONIZED)
300+
->assertNow();
295301
}
296302

297303
// https://github.com/symfony/symfony/issues/4359
@@ -543,9 +549,11 @@ public function testViolationIfExtraData()
543549

544550
$this->validator->validate($form, new Form());
545551

546-
$this->assertViolation('Extra!', array(
547-
'{{ extra_fields }}' => 'foo',
548-
), 'property.path', array('foo' => 'bar'));
552+
$this->assertViolation('Extra!')
553+
->withParameter('{{ extra_fields }}', 'foo')
554+
->withInvalidValue(array('foo' => 'bar'))
555+
->withCode(Form::ERROR_NO_SUCH_FIELD)
556+
->assertNow();
549557
}
550558

551559
public function testNoViolationIfAllowExtraData()

src/Symfony/Component/Form/Tests/Extension/Validator/EventListener/ValidationListenerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function testMapViolation()
109109

110110
public function testMapViolationAllowsNonSyncIfInvalid()
111111
{
112-
$violation = $this->getConstraintViolation(Form::ERR_INVALID);
112+
$violation = $this->getConstraintViolation(Form::ERROR_NOT_SYNCHRONIZED);
113113
$form = $this->getForm('street');
114114

115115
$this->validator->expects($this->once())

src/Symfony/Component/Validator/Constraint.php

Lines 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

Lines 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 <[email protected]>
23+
* @author Bernhard Schussek <[email protected]>
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

Lines 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

Lines 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;

0 commit comments

Comments
 (0)