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 97243bc

Browse filesBrowse files
committed
[Validator] Fixed value-to-string conversion in constraint violations
1 parent 75e8815 commit 97243bc
Copy full SHA for 97243bc

32 files changed

+202
-128
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/ConstraintValidator.php
+63Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,67 @@ public function initialize(ExecutionContextInterface $context)
3232
{
3333
$this->context = $context;
3434
}
35+
36+
/**
37+
* Returns a string representation of the type of the value.
38+
*
39+
* @param mixed $value
40+
*
41+
* @return string
42+
*/
43+
protected function valueToType($value)
44+
{
45+
return is_object($value) ? get_class($value) : gettype($value);
46+
}
47+
48+
/**
49+
* Returns a string representation of the value.
50+
*
51+
* @param mixed $value
52+
*
53+
* @return string
54+
*/
55+
protected function valueToString($value)
56+
{
57+
if ($value instanceof \DateTime) {
58+
if (class_exists('IntlDateFormatter')) {
59+
$locale = \Locale::getDefault();
60+
$formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
61+
62+
return $formatter->format($value);
63+
}
64+
65+
return $value->format('Y-m-d H:i:s');
66+
}
67+
68+
if (is_object($value)) {
69+
return 'Object('.get_class($value).')';
70+
}
71+
72+
if (is_array($value)) {
73+
return 'Array';
74+
}
75+
76+
if (is_string($value)) {
77+
return '"'.$value.'"';
78+
}
79+
80+
if (is_resource($value)) {
81+
return sprintf('Resource(%s#%d)', get_resource_type($value), $value);
82+
}
83+
84+
if (null === $value) {
85+
return 'null';
86+
}
87+
88+
if (false === $value) {
89+
return 'false';
90+
}
91+
92+
if (true === $value) {
93+
return 'true';
94+
}
95+
96+
return (string) $value;
97+
}
3598
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/ConstraintViolation.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ public function __construct($message, $messageTemplate, array $messageParameters
9696
public function __toString()
9797
{
9898
if (is_object($this->root)) {
99-
$class = get_class($this->root);
99+
$class = 'Object('.get_class($this->root).')';
100100
} elseif (is_array($this->root)) {
101-
$class = "Array";
101+
$class = 'Array';
102102
} else {
103103
$class = (string) $this->root;
104104
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php
-28Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,6 @@ public function validate($value, Constraint $constraint)
3939
}
4040
}
4141

42-
/**
43-
* Returns a string representation of the type of the value.
44-
*
45-
* @param mixed $value
46-
*
47-
* @return string
48-
*/
49-
private function valueToType($value)
50-
{
51-
return is_object($value) ? get_class($value) : gettype($value);
52-
}
53-
54-
/**
55-
* Returns a string representation of the value.
56-
*
57-
* @param mixed $value
58-
*
59-
* @return string
60-
*/
61-
private function valueToString($value)
62-
{
63-
if ($value instanceof \DateTime) {
64-
return $value->format('Y-m-d H:i:s');
65-
}
66-
67-
return var_export($value, true);
68-
}
69-
7042
/**
7143
* Compares the two given values to find if their relationship is valid
7244
*

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/BlankValidator.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ class BlankValidator extends ConstraintValidator
2727
public function validate($value, Constraint $constraint)
2828
{
2929
if ('' !== $value && null !== $value) {
30-
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
30+
$this->context->addViolation($constraint->message, array(
31+
'{{ value }}' => $this->valueToString($value)
32+
));
3133
}
3234
}
3335
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ public function validate($value, Constraint $constraint)
108108
}
109109

110110
if (!is_numeric($value)) {
111-
$this->context->addViolation($constraint->message);
111+
$this->context->addViolation($constraint->message, array(
112+
'{{ value }}' => $this->valueToString($value),
113+
));
112114

113115
return;
114116
}
@@ -124,6 +126,8 @@ public function validate($value, Constraint $constraint)
124126
}
125127
}
126128

127-
$this->context->addViolation($constraint->message);
129+
$this->context->addViolation($constraint->message, array(
130+
'{{ value }}' => $value,
131+
));
128132
}
129133
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/ChoiceValidator.php
+12-4Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,33 @@ public function validate($value, Constraint $constraint)
5959
if ($constraint->multiple) {
6060
foreach ($value as $_value) {
6161
if (!in_array($_value, $choices, $constraint->strict)) {
62-
$this->context->addViolation($constraint->multipleMessage, array('{{ value }}' => $_value));
62+
$this->context->addViolation($constraint->multipleMessage, array(
63+
'{{ value }}' => $this->valueToString($_value),
64+
));
6365
}
6466
}
6567

6668
$count = count($value);
6769

6870
if ($constraint->min !== null && $count < $constraint->min) {
69-
$this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), null, (int) $constraint->min);
71+
$this->context->addViolation($constraint->minMessage, array(
72+
'{{ limit }}' => $constraint->min
73+
), null, (int) $constraint->min);
7074

7175
return;
7276
}
7377

7478
if ($constraint->max !== null && $count > $constraint->max) {
75-
$this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), null, (int) $constraint->max);
79+
$this->context->addViolation($constraint->maxMessage, array(
80+
'{{ limit }}' => $constraint->max
81+
), null, (int) $constraint->max);
7682

7783
return;
7884
}
7985
} elseif (!in_array($value, $choices, $constraint->strict)) {
80-
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
86+
$this->context->addViolation($constraint->message, array(
87+
'{{ value }}' => $this->valueToString($value)
88+
));
8189
}
8290
}
8391
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/CountryValidator.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public function validate($value, Constraint $constraint)
4242
$countries = Intl::getRegionBundle()->getCountryNames();
4343

4444
if (!isset($countries[$value])) {
45-
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
45+
$this->context->addViolation($constraint->message, array(
46+
'{{ value }}' => $value,
47+
));
4648
}
4749
}
4850
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/CurrencyValidator.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public function validate($value, Constraint $constraint)
4242
$currencies = Intl::getCurrencyBundle()->getCurrencyNames();
4343

4444
if (!isset($currencies[$value])) {
45-
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
45+
$this->context->addViolation($constraint->message, array(
46+
'{{ value }}' => $value,
47+
));
4648
}
4749
}
4850
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/DateValidator.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public function validate($value, Constraint $constraint)
4040
$value = (string) $value;
4141

4242
if (!preg_match(static::PATTERN, $value, $matches) || !checkdate($matches[2], $matches[3], $matches[1])) {
43-
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
43+
$this->context->addViolation($constraint->message, array(
44+
'{{ value }}' => $value,
45+
));
4446
}
4547
}
4648
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/EmailValidator.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public function validate($value, Constraint $constraint)
5050
}
5151

5252
if (!$valid) {
53-
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
53+
$this->context->addViolation($constraint->message, array(
54+
'{{ value }}' => $value,
55+
));
5456
}
5557
}
5658

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/FileValidator.php
+7-7Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ public function validate($value, Constraint $constraint)
126126

127127
if ($size > $limit) {
128128
$this->context->addViolation($constraint->maxSizeMessage, array(
129-
'{{ size }}' => $size,
130-
'{{ limit }}' => $limit,
131-
'{{ suffix }}' => $suffix,
132-
'{{ file }}' => $path,
129+
'{{ size }}' => $size,
130+
'{{ limit }}' => $limit,
131+
'{{ suffix }}' => $suffix,
132+
'{{ file }}' => $path,
133133
));
134134

135135
return;
@@ -161,9 +161,9 @@ public function validate($value, Constraint $constraint)
161161

162162
if (false === $valid) {
163163
$this->context->addViolation($constraint->mimeTypesMessage, array(
164-
'{{ type }}' => '"'.$mime.'"',
165-
'{{ types }}' => '"'.implode('", "', $mimeTypes) .'"',
166-
'{{ file }}' => $path,
164+
'{{ type }}' => '"'.$mime.'"',
165+
'{{ types }}' => '"'.implode('", "', $mimeTypes) .'"',
166+
'{{ file }}' => $path,
167167
));
168168
}
169169
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/IpValidator.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ public function validate($value, Constraint $constraint)
9191
}
9292

9393
if (!filter_var($value, FILTER_VALIDATE_IP, $flag)) {
94-
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
94+
$this->context->addViolation($constraint->message, array(
95+
'{{ value }}' => $value,
96+
));
9597
}
9698
}
9799
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/LanguageValidator.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public function validate($value, Constraint $constraint)
4242
$languages = Intl::getLanguageBundle()->getLanguageNames();
4343

4444
if (!isset($languages[$value])) {
45-
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
45+
$this->context->addViolation($constraint->message, array(
46+
'{{ value }}' => $value,
47+
));
4648
}
4749
}
4850
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/LocaleValidator.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public function validate($value, Constraint $constraint)
4242
$locales = Intl::getLocaleBundle()->getLocaleNames();
4343

4444
if (!isset($locales[$value])) {
45-
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
45+
$this->context->addViolation($constraint->message, array(
46+
'{{ value }}' => $value,
47+
));
4648
}
4749
}
4850
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/NullValidator.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public function validate($value, Constraint $constraint)
3333
$value = 'Array';
3434
}
3535

36-
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
36+
$this->context->addViolation($constraint->message, array(
37+
'{{ value }}' => $value,
38+
));
3739
}
3840
}
3941
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/RangeValidator.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function validate($value, Constraint $constraint)
3030

3131
if (!is_numeric($value)) {
3232
$this->context->addViolation($constraint->invalidMessage, array(
33-
'{{ value }}' => $value,
33+
'{{ value }}' => $this->valueToString($value),
3434
));
3535

3636
return;

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/RegexValidator.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public function validate($value, Constraint $constraint)
4141
$value = (string) $value;
4242

4343
if ($constraint->match xor preg_match($constraint->pattern, $value)) {
44-
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
44+
$this->context->addViolation($constraint->message, array(
45+
'{{ value }}' => $value,
46+
));
4547
}
4648
}
4749
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/TimeValidator.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public function validate($value, Constraint $constraint)
4040
$value = (string) $value;
4141

4242
if (!preg_match(static::PATTERN, $value)) {
43-
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
43+
$this->context->addViolation($constraint->message, array(
44+
'{{ value }}' => $value,
45+
));
4446
}
4547
}
4648
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/TypeValidator.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function validate($value, Constraint $constraint)
4444
}
4545

4646
$this->context->addViolation($constraint->message, array(
47-
'{{ value }}' => is_object($value) ? get_class($value) : (is_array($value) ? 'Array' : (string) $value),
47+
'{{ value }}' => $this->valueToString($value),
4848
'{{ type }}' => $constraint->type,
4949
));
5050
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/UrlValidator.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public function validate($value, Constraint $constraint)
5555
$pattern = sprintf(static::PATTERN, implode('|', $constraint->protocols));
5656

5757
if (!preg_match($pattern, $value)) {
58-
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
58+
$this->context->addViolation($constraint->message, array(
59+
'{{ value }}' => $value,
60+
));
5961
}
6062
}
6163
}

‎src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ protected function setUp()
2929
->disableOriginalConstructor()
3030
->getMock();
3131
$this->validator->initialize($this->context);
32+
33+
\Locale::setDefault('en');
3234
}
3335

3436
/**
@@ -75,7 +77,7 @@ abstract public function provideValidComparisons();
7577
* @param mixed $comparedValueString
7678
* @param string $comparedValueType
7779
*/
78-
public function testInvalidComparisonToValue($dirtyValue, $comparedValue, $comparedValueString, $comparedValueType)
80+
public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType)
7981
{
8082
$constraint = $this->createConstraint(array('value' => $comparedValue));
8183
$constraint->message = 'Constraint Message';
@@ -87,7 +89,7 @@ public function testInvalidComparisonToValue($dirtyValue, $comparedValue, $compa
8789
$this->context->expects($this->once())
8890
->method('addViolation')
8991
->with('Constraint Message', array(
90-
'{{ value }}' => $comparedValueString,
92+
'{{ value }}' => $dirtyValueAsString,
9193
'{{ compared_value }}' => $comparedValueString,
9294
'{{ compared_value_type }}' => $comparedValueType
9395
));

0 commit comments

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