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 ec5ec81

Browse filesBrowse files
committed
[Validator] fixed deprecation notices for BuildViolation() calls in constraints
1 parent fb3f9d2 commit ec5ec81
Copy full SHA for ec5ec81
Expand file treeCollapse file tree

38 files changed

+1118
-400
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php
+12-4Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Doctrine\Validator\Constraints;
1313

1414
use Doctrine\Common\Persistence\ManagerRegistry;
15+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1516
use Symfony\Component\Validator\Constraint;
1617
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1718
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
@@ -133,9 +134,16 @@ public function validate($entity, Constraint $constraint)
133134

134135
$errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];
135136

136-
$this->buildViolation($constraint->message)
137-
->atPath($errorPath)
138-
->setInvalidValue($criteria[$fields[0]])
139-
->addViolation();
137+
if ($this->context instanceof ExecutionContextInterface) {
138+
$this->context->buildViolation($constraint->message)
139+
->atPath($errorPath)
140+
->setInvalidValue($criteria[$fields[0]])
141+
->addViolation();
142+
} else {
143+
$this->buildViolation($constraint->message)
144+
->atPath($errorPath)
145+
->setInvalidValue($criteria[$fields[0]])
146+
->addViolation();
147+
}
140148
}
141149
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php
+28-11Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,39 @@ public function validate($form, Constraint $constraint)
9999
? (string) $form->getViewData()
100100
: gettype($form->getViewData());
101101

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::NOT_SYNCHRONIZED_ERROR)
106-
->setCause($form->getTransformationFailure())
107-
->addViolation();
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::NOT_SYNCHRONIZED_ERROR)
107+
->setCause($form->getTransformationFailure())
108+
->addViolation();
109+
} else {
110+
$this->buildViolation($config->getOption('invalid_message'))
111+
->setParameters(array_replace(array('{{ value }}' => $clientDataAsString), $config->getOption('invalid_message_parameters')))
112+
->setInvalidValue($form->getViewData())
113+
->setCode(Form::NOT_SYNCHRONIZED_ERROR)
114+
->setCause($form->getTransformationFailure())
115+
->addViolation();
116+
}
108117
}
109118
}
110119

111120
// Mark the form with an error if it contains extra fields
112121
if (!$config->getOption('allow_extra_fields') && count($form->getExtraData()) > 0) {
113-
$this->buildViolation($config->getOption('extra_fields_message'))
114-
->setParameter('{{ extra_fields }}', implode('", "', array_keys($form->getExtraData())))
115-
->setInvalidValue($form->getExtraData())
116-
->setCode(Form::NO_SUCH_FIELD_ERROR)
117-
->addViolation();
122+
if ($this->context instanceof ExecutionContextInterface) {
123+
$this->context->buildViolation($config->getOption('extra_fields_message'))
124+
->setParameter('{{ extra_fields }}', implode('", "', array_keys($form->getExtraData())))
125+
->setInvalidValue($form->getExtraData())
126+
->setCode(Form::NO_SUCH_FIELD_ERROR)
127+
->addViolation();
128+
} else {
129+
$this->buildViolation($config->getOption('extra_fields_message'))
130+
->setParameter('{{ extra_fields }}', implode('", "', array_keys($form->getExtraData())))
131+
->setInvalidValue($form->getExtraData())
132+
->setCode(Form::NO_SUCH_FIELD_ERROR)
133+
->addViolation();
134+
}
118135
}
119136
}
120137

‎src/Symfony/Component/Security/Core/Validator/Constraints/UserPasswordValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Core/Validator/Constraints/UserPasswordValidator.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Security\Core\User\UserInterface;
1515
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
1616
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
17+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1718
use Symfony\Component\Validator\Constraint;
1819
use Symfony\Component\Validator\ConstraintValidator;
1920
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

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

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

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1415
use Symfony\Component\Validator\Constraint;
1516
use Symfony\Component\Validator\ConstraintValidator;
1617
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@@ -54,11 +55,19 @@ public function validate($value, Constraint $constraint)
5455
}
5556

5657
if (!$this->compareValues($value, $comparedValue)) {
57-
$this->buildViolation($constraint->message)
58-
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
59-
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
60-
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
61-
->addViolation();
58+
if ($this->context instanceof ExecutionContextInterface) {
59+
$this->context->buildViolation($constraint->message)
60+
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
61+
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
62+
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
63+
->addViolation();
64+
} else {
65+
$this->buildViolation($constraint->message)
66+
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
67+
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
68+
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
69+
->addViolation();
70+
}
6271
}
6372
}
6473

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

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

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1415
use Symfony\Component\Validator\Constraint;
1516
use Symfony\Component\Validator\ConstraintValidator;
1617
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@@ -32,9 +33,15 @@ public function validate($value, Constraint $constraint)
3233
}
3334

3435
if ('' !== $value && null !== $value) {
35-
$this->buildViolation($constraint->message)
36-
->setParameter('{{ value }}', $this->formatValue($value))
37-
->addViolation();
36+
if ($this->context instanceof ExecutionContextInterface) {
37+
$this->context->buildViolation($constraint->message)
38+
->setParameter('{{ value }}', $this->formatValue($value))
39+
->addViolation();
40+
} else {
41+
$this->buildViolation($constraint->message)
42+
->setParameter('{{ value }}', $this->formatValue($value))
43+
->addViolation();
44+
}
3845
}
3946
}
4047
}

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

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

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1415
use Symfony\Component\Validator\Constraint;
1516
use Symfony\Component\Validator\ConstraintValidator;
1617
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

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

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

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1415
use Symfony\Component\Validator\Constraint;
1516
use Symfony\Component\Validator\ConstraintValidator;
1617
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@@ -115,10 +116,17 @@ public function validate($value, Constraint $constraint)
115116
}
116117

117118
if (!is_numeric($value)) {
118-
$this->buildViolation($constraint->message)
119-
->setParameter('{{ value }}', $this->formatValue($value))
120-
->setCode(CardScheme::NOT_NUMERIC_ERROR)
121-
->addViolation();
119+
if ($this->context instanceof ExecutionContextInterface) {
120+
$this->context->buildViolation($constraint->message)
121+
->setParameter('{{ value }}', $this->formatValue($value))
122+
->setCode(CardScheme::NOT_NUMERIC_ERROR)
123+
->addViolation();
124+
} else {
125+
$this->buildViolation($constraint->message)
126+
->setParameter('{{ value }}', $this->formatValue($value))
127+
->setCode(CardScheme::NOT_NUMERIC_ERROR)
128+
->addViolation();
129+
}
122130

123131
return;
124132
}
@@ -134,9 +142,16 @@ public function validate($value, Constraint $constraint)
134142
}
135143
}
136144

137-
$this->buildViolation($constraint->message)
138-
->setParameter('{{ value }}', $this->formatValue($value))
139-
->setCode(CardScheme::INVALID_FORMAT_ERROR)
140-
->addViolation();
145+
if ($this->context instanceof ExecutionContextInterface) {
146+
$this->context->buildViolation($constraint->message)
147+
->setParameter('{{ value }}', $this->formatValue($value))
148+
->setCode(CardScheme::INVALID_FORMAT_ERROR)
149+
->addViolation();
150+
} else {
151+
$this->buildViolation($constraint->message)
152+
->setParameter('{{ value }}', $this->formatValue($value))
153+
->setCode(CardScheme::INVALID_FORMAT_ERROR)
154+
->addViolation();
155+
}
141156
}
142157
}

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

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

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1415
use Symfony\Component\Validator\Constraint;
1516
use Symfony\Component\Validator\ConstraintValidator;
1617
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
@@ -62,11 +63,19 @@ public function validate($value, Constraint $constraint)
6263
if ($constraint->multiple) {
6364
foreach ($value as $_value) {
6465
if (!in_array($_value, $choices, $constraint->strict)) {
65-
$this->buildViolation($constraint->multipleMessage)
66-
->setParameter('{{ value }}', $this->formatValue($_value))
67-
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
68-
->setInvalidValue($_value)
69-
->addViolation();
66+
if ($this->context instanceof ExecutionContextInterface) {
67+
$this->context->buildViolation($constraint->multipleMessage)
68+
->setParameter('{{ value }}', $this->formatValue($_value))
69+
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
70+
->setInvalidValue($_value)
71+
->addViolation();
72+
} else {
73+
$this->buildViolation($constraint->multipleMessage)
74+
->setParameter('{{ value }}', $this->formatValue($_value))
75+
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
76+
->setInvalidValue($_value)
77+
->addViolation();
78+
}
7079

7180
return;
7281
}
@@ -75,29 +84,52 @@ public function validate($value, Constraint $constraint)
7584
$count = count($value);
7685

7786
if ($constraint->min !== null && $count < $constraint->min) {
78-
$this->buildViolation($constraint->minMessage)
79-
->setParameter('{{ limit }}', $constraint->min)
80-
->setPlural((int) $constraint->min)
81-
->setCode(Choice::TOO_FEW_ERROR)
82-
->addViolation();
87+
if ($this->context instanceof ExecutionContextInterface) {
88+
$this->context->buildViolation($constraint->minMessage)
89+
->setParameter('{{ limit }}', $constraint->min)
90+
->setPlural((int) $constraint->min)
91+
->setCode(Choice::TOO_FEW_ERROR)
92+
->addViolation();
93+
} else {
94+
$this->buildViolation($constraint->minMessage)
95+
->setParameter('{{ limit }}', $constraint->min)
96+
->setPlural((int) $constraint->min)
97+
->setCode(Choice::TOO_FEW_ERROR)
98+
->addViolation();
99+
}
83100

84101
return;
85102
}
86103

87104
if ($constraint->max !== null && $count > $constraint->max) {
88-
$this->buildViolation($constraint->maxMessage)
89-
->setParameter('{{ limit }}', $constraint->max)
90-
->setPlural((int) $constraint->max)
91-
->setCode(Choice::TOO_MANY_ERROR)
92-
->addViolation();
105+
if ($this->context instanceof ExecutionContextInterface) {
106+
$this->context->buildViolation($constraint->maxMessage)
107+
->setParameter('{{ limit }}', $constraint->max)
108+
->setPlural((int) $constraint->max)
109+
->setCode(Choice::TOO_MANY_ERROR)
110+
->addViolation();
111+
} else {
112+
$this->buildViolation($constraint->maxMessage)
113+
->setParameter('{{ limit }}', $constraint->max)
114+
->setPlural((int) $constraint->max)
115+
->setCode(Choice::TOO_MANY_ERROR)
116+
->addViolation();
117+
}
93118

94119
return;
95120
}
96121
} elseif (!in_array($value, $choices, $constraint->strict)) {
97-
$this->buildViolation($constraint->message)
98-
->setParameter('{{ value }}', $this->formatValue($value))
99-
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
100-
->addViolation();
122+
if ($this->context instanceof ExecutionContextInterface) {
123+
$this->context->buildViolation($constraint->message)
124+
->setParameter('{{ value }}', $this->formatValue($value))
125+
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
126+
->addViolation();
127+
} else {
128+
$this->buildViolation($constraint->message)
129+
->setParameter('{{ value }}', $this->formatValue($value))
130+
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
131+
->addViolation();
132+
}
101133
}
102134
}
103135
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/CollectionValidator.php
+15-6Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,21 @@ public function validate($value, Constraint $constraint)
6969
}
7070
}
7171
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
72-
$this->buildViolationInContext($context, $constraint->missingFieldsMessage)
73-
->atPath('['.$field.']')
74-
->setParameter('{{ field }}', $this->formatValue($field))
75-
->setInvalidValue(null)
76-
->setCode(Collection::MISSING_FIELD_ERROR)
77-
->addViolation();
72+
if ($context instanceof ExecutionContextInterface) {
73+
$context->buildViolation($constraint->missingFieldsMessage)
74+
->atPath('['.$field.']')
75+
->setParameter('{{ field }}', $this->formatValue($field))
76+
->setInvalidValue(null)
77+
->setCode(Collection::MISSING_FIELD_ERROR)
78+
->addViolation();
79+
} else {
80+
$this->buildViolationInContext($context, $constraint->missingFieldsMessage)
81+
->atPath('['.$field.']')
82+
->setParameter('{{ field }}', $this->formatValue($field))
83+
->setInvalidValue(null)
84+
->setCode(Collection::MISSING_FIELD_ERROR)
85+
->addViolation();
86+
}
7887
}
7988
}
8089

0 commit comments

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