Skip to content

Commit d7615e7

Browse files
committed
bug #12016 [Validator] Added ConstraintValidator::buildViolation() helper for BC with the 2.4 API (webmozart)
This PR was merged into the 2.5 branch. Discussion ---------- [Validator] Added ConstraintValidator::buildViolation() helper for BC with the 2.4 API | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR adds a `buildViolation()` helper method to the base `ConstraintValidator` to remove the API checks (2.4 vs. 2.5) from the constraint validator implementations. Once the 2.4 API is removed, this helper method will be removed as well. **Todos** - [x] Backport changes from #12021 Commits ------- 6b0c24a [Validator] Added ConstraintValidator::buildViolation() helper for BC with 2.4 API
2 parents 07b234e + 6b0c24a commit d7615e7

Some content is hidden

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

53 files changed

+898
-627
lines changed

src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,19 @@ public function testValidateUniquenessAfterConsideringMultipleQueryResults()
262262

263263
$this->validator->validate($entity1, $constraint);
264264

265-
$this->assertViolation('myMessage', array(), 'property.path.name', 'Foo');
265+
$this->buildViolation('myMessage')
266+
->atPath('property.path.name')
267+
->setInvalidValue('Foo')
268+
->assertRaised();
269+
266270
$this->context->getViolations()->remove(0);
267271

268272
$this->validator->validate($entity2, $constraint);
269273

270-
$this->assertViolation('myMessage', array(), 'property.path.name', 'Foo');
274+
$this->buildViolation('myMessage')
275+
->atPath('property.path.name')
276+
->setInvalidValue('Foo')
277+
->assertRaised();
271278
}
272279

273280
public function testValidateUniquenessUsingCustomRepositoryMethod()

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Doctrine\Common\Persistence\ManagerRegistry;
1515
use Symfony\Component\Validator\Constraint;
16-
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1716
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1817
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1918
use Symfony\Component\Validator\ConstraintValidator;
@@ -137,14 +136,9 @@ public function validate($entity, Constraint $constraint)
137136

138137
$errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];
139138

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-
}
139+
$this->buildViolation($constraint->message)
140+
->atPath($errorPath)
141+
->setInvalidValue($criteria[$fields[0]])
142+
->addViolation();
149143
}
150144
}

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

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -99,40 +99,20 @@ public function validate($form, Constraint $constraint)
9999
? (string) $form->getViewData()
100100
: gettype($form->getViewData());
101101

102-
if ($this->context instanceof ExecutionContextInterface) {
103-
$this->context->buildViolation($config->getOption('invalid_message'))
104-
->setParameters(array_replace(array('{{ value }}' => $clientDataAsString), $config->getOption('invalid_message_parameters')))
105-
->setInvalidValue($form->getViewData())
106-
->setCode(Form::ERR_INVALID)
107-
->addViolation();
108-
} else {
109-
// 2.4 API
110-
$this->context->addViolation(
111-
$config->getOption('invalid_message'),
112-
array_replace(array('{{ value }}' => $clientDataAsString), $config->getOption('invalid_message_parameters')),
113-
$form->getViewData(),
114-
null,
115-
Form::ERR_INVALID
116-
);
117-
}
102+
$this->buildViolation($config->getOption('invalid_message'))
103+
->setParameters(array_replace(array('{{ value }}' => $clientDataAsString), $config->getOption('invalid_message_parameters')))
104+
->setInvalidValue($form->getViewData())
105+
->setCode(Form::ERR_INVALID)
106+
->addViolation();
118107
}
119108
}
120109

121110
// Mark the form with an error if it contains extra fields
122111
if (count($form->getExtraData()) > 0) {
123-
if ($this->context instanceof ExecutionContextInterface) {
124-
$this->context->buildViolation($config->getOption('extra_fields_message'))
125-
->setParameter('{{ extra_fields }}', implode('", "', array_keys($form->getExtraData())))
126-
->setInvalidValue($form->getExtraData())
127-
->addViolation();
128-
} else {
129-
// 2.4 API
130-
$this->context->addViolation(
131-
$config->getOption('extra_fields_message'),
132-
array('{{ extra_fields }}' => implode('", "', array_keys($form->getExtraData()))),
133-
$form->getExtraData()
134-
);
135-
}
112+
$this->buildViolation($config->getOption('extra_fields_message'))
113+
->setParameter('{{ extra_fields }}', implode('", "', array_keys($form->getExtraData())))
114+
->setInvalidValue($form->getExtraData())
115+
->addViolation();
136116
}
137117
}
138118

src/Symfony/Component/Security/Core/Tests/Validator/Constraints/UserPasswordValidatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ public function testPasswordIsNotValid()
9292

9393
$this->validator->validate('secret', $constraint);
9494

95-
$this->assertViolation('myMessage');
95+
$this->buildViolation('myMessage')
96+
->assertRaised();
9697
}
9798

9899
/**

src/Symfony/Component/Validator/ConstraintValidator.php

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
namespace Symfony\Component\Validator;
1313

14+
use Symfony\Component\Validator\Context\ExecutionContextInterface as ExecutionContextInterface2Dot5;
15+
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
16+
use Symfony\Component\Validator\Violation\LegacyConstraintViolationBuilder;
17+
1418
/**
1519
* Base class for constraint validators
1620
*
@@ -24,14 +28,14 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
2428
* Whether to format {@link \DateTime} objects as RFC-3339 dates
2529
* ("Y-m-d H:i:s").
2630
*
27-
* @var integer
31+
* @var int
2832
*/
2933
const PRETTY_DATE = 1;
3034

3135
/**
3236
* Whether to cast objects with a "__toString()" method to strings.
3337
*
34-
* @var integer
38+
* @var int
3539
*/
3640
const OBJECT_TO_STRING = 2;
3741

@@ -48,6 +52,47 @@ public function initialize(ExecutionContextInterface $context)
4852
$this->context = $context;
4953
}
5054

55+
/**
56+
* Wrapper for {@link ExecutionContextInterface::buildViolation} that
57+
* supports the 2.4 context API.
58+
*
59+
* @param string $message The violation message
60+
* @param array $parameters The message parameters
61+
*
62+
* @return ConstraintViolationBuilderInterface The violation builder
63+
*
64+
* @deprecated This method will be removed in Symfony 3.0.
65+
*/
66+
protected function buildViolation($message, array $parameters = array())
67+
{
68+
if ($this->context instanceof ExecutionContextInterface2Dot5) {
69+
return $this->context->buildViolation($message, $parameters);
70+
}
71+
72+
return new LegacyConstraintViolationBuilder($this->context, $message, $parameters);
73+
}
74+
75+
/**
76+
* Wrapper for {@link ExecutionContextInterface::buildViolation} that
77+
* supports the 2.4 context API.
78+
*
79+
* @param ExecutionContextInterface $context The context to use
80+
* @param string $message The violation message
81+
* @param array $parameters The message parameters
82+
*
83+
* @return ConstraintViolationBuilderInterface The violation builder
84+
*
85+
* @deprecated This method will be removed in Symfony 3.0.
86+
*/
87+
protected function buildViolationInContext(ExecutionContextInterface $context, $message, array $parameters = array())
88+
{
89+
if ($context instanceof ExecutionContextInterface2Dot5) {
90+
return $context->buildViolation($message, $parameters);
91+
}
92+
93+
return new LegacyConstraintViolationBuilder($context, $message, $parameters);
94+
}
95+
5196
/**
5297
* Returns a string representation of the type of the value.
5398
*
@@ -82,7 +127,7 @@ protected function formatTypeOf($value)
82127
* confused by the violation message.
83128
*
84129
* @param mixed $value The value to format as string
85-
* @param integer $format A bitwise combination of the format
130+
* @param int $format A bitwise combination of the format
86131
* constants in this class
87132
*
88133
* @return string The string representation of the passed value
@@ -142,7 +187,7 @@ protected function formatValue($value, $format = 0)
142187
* {@link formatValue()}. The values are then concatenated with commas.
143188
*
144189
* @param array $values A list of values
145-
* @param integer $format A bitwise combination of the format
190+
* @param int $format A bitwise combination of the format
146191
* constants in this class
147192
*
148193
* @return string The string representation of the value list

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Provides a base class for the validation of property comparisons.
2020
*
2121
* @author Daniel Holmes <daniel@danielholmes.org>
22+
* @author Bernhard Schussek <bschussek@gmail.com>
2223
*/
2324
abstract class AbstractComparisonValidator extends ConstraintValidator
2425
{
@@ -35,12 +36,14 @@ public function validate($value, Constraint $constraint)
3536
return;
3637
}
3738

38-
if (!$this->compareValues($value, $constraint->value)) {
39-
$this->context->addViolation($constraint->message, array(
40-
'{{ value }}' => $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE),
41-
'{{ compared_value }}' => $this->formatValue($constraint->value, self::OBJECT_TO_STRING | self::PRETTY_DATE),
42-
'{{ compared_value_type }}' => $this->formatTypeOf($constraint->value),
43-
));
39+
$comparedValue = $constraint->value;
40+
41+
if (!$this->compareValues($value, $comparedValue)) {
42+
$this->buildViolation($constraint->message)
43+
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
44+
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
45+
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
46+
->addViolation();
4447
}
4548
}
4649

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public function validate($value, Constraint $constraint)
3232
}
3333

3434
if ('' !== $value && null !== $value) {
35-
$this->context->addViolation($constraint->message, array(
36-
'{{ value }}' => $this->formatValue($value),
37-
));
35+
$this->buildViolation($constraint->message)
36+
->setParameter('{{ value }}', $this->formatValue($value))
37+
->addViolation();
3838
}
3939
}
4040
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*
1919
* @Annotation
2020
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
21+
*
22+
* @author Tim Nagel <t.nagel@infinite.net.au>
2123
*/
2224
class CardScheme extends Constraint
2325
{

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
/**
1919
* Validates that a card number belongs to a specified scheme.
2020
*
21+
* @author Tim Nagel <t.nagel@infinite.net.au>
22+
* @author Bernhard Schussek <bschussek@gmail.com>
23+
*
2124
* @see http://en.wikipedia.org/wiki/Bank_card_number
2225
* @see http://www.regular-expressions.info/creditcard.html
23-
* @author Tim Nagel <t.nagel@infinite.net.au>
2426
*/
2527
class CardSchemeValidator extends ConstraintValidator
2628
{
@@ -113,9 +115,9 @@ public function validate($value, Constraint $constraint)
113115
}
114116

115117
if (!is_numeric($value)) {
116-
$this->context->addViolation($constraint->message, array(
117-
'{{ value }}' => $this->formatValue($value),
118-
));
118+
$this->buildViolation($constraint->message)
119+
->setParameter('{{ value }}', $this->formatValue($value))
120+
->addViolation();
119121

120122
return;
121123
}
@@ -131,8 +133,8 @@ public function validate($value, Constraint $constraint)
131133
}
132134
}
133135

134-
$this->context->addViolation($constraint->message, array(
135-
'{{ value }}' => $this->formatValue($value),
136-
));
136+
$this->buildViolation($constraint->message)
137+
->setParameter('{{ value }}', $this->formatValue($value))
138+
->addViolation();
137139
}
138140
}

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

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16-
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1716
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1817
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1918

@@ -64,63 +63,38 @@ public function validate($value, Constraint $constraint)
6463
if ($constraint->multiple) {
6564
foreach ($value as $_value) {
6665
if (!in_array($_value, $choices, $constraint->strict)) {
67-
if ($this->context instanceof ExecutionContextInterface) {
68-
$this->context->buildViolation($constraint->multipleMessage)
69-
->setParameter('{{ value }}', $this->formatValue($_value))
70-
->addViolation();
71-
} else {
72-
// 2.4 API
73-
$this->context->addViolation($constraint->multipleMessage, array(
74-
'{{ value }}' => $this->formatValue($_value),
75-
));
76-
}
66+
$this->buildViolation($constraint->multipleMessage)
67+
->setParameter('{{ value }}', $this->formatValue($_value))
68+
->setInvalidValue($_value)
69+
->addViolation();
70+
71+
return;
7772
}
7873
}
7974

8075
$count = count($value);
8176

8277
if ($constraint->min !== null && $count < $constraint->min) {
83-
if ($this->context instanceof ExecutionContextInterface) {
84-
$this->context->buildViolation($constraint->minMessage)
85-
->setParameter('{{ limit }}', $constraint->min)
86-
->setPlural((int) $constraint->min)
87-
->addViolation();
88-
} else {
89-
// 2.4 API
90-
$this->context->addViolation($constraint->minMessage, array(
91-
'{{ limit }}' => $constraint->min,
92-
), $value, (int) $constraint->min);
93-
}
78+
$this->buildViolation($constraint->minMessage)
79+
->setParameter('{{ limit }}', $constraint->min)
80+
->setPlural((int) $constraint->min)
81+
->addViolation();
9482

9583
return;
9684
}
9785

9886
if ($constraint->max !== null && $count > $constraint->max) {
99-
if ($this->context instanceof ExecutionContextInterface) {
100-
$this->context->buildViolation($constraint->maxMessage)
101-
->setParameter('{{ limit }}', $constraint->max)
102-
->setPlural((int) $constraint->max)
103-
->addViolation();
104-
} else {
105-
// 2.4 API
106-
$this->context->addViolation($constraint->maxMessage, array(
107-
'{{ limit }}' => $constraint->max,
108-
), $value, (int) $constraint->max);
109-
}
87+
$this->buildViolation($constraint->maxMessage)
88+
->setParameter('{{ limit }}', $constraint->max)
89+
->setPlural((int) $constraint->max)
90+
->addViolation();
11091

11192
return;
11293
}
11394
} elseif (!in_array($value, $choices, $constraint->strict)) {
114-
if ($this->context instanceof ExecutionContextInterface) {
115-
$this->context->buildViolation($constraint->message)
116-
->setParameter('{{ value }}', $this->formatValue($value))
117-
->addViolation();
118-
} else {
119-
// 2.4 API
120-
$this->context->addViolation($constraint->message, array(
121-
'{{ value }}' => $this->formatValue($value),
122-
));
123-
}
95+
$this->buildViolation($constraint->message)
96+
->setParameter('{{ value }}', $this->formatValue($value))
97+
->addViolation();
12498
}
12599
}
126100
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy