8000 [Validator] Fixed value-to-string conversion in constraint violations · webmozart/symfony@97243bc · GitHub
[go: up one dir, main page]

Skip to content

Commit 97243bc

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

32 files changed

+202
-128
lines changed

src/Symfony/Component/Validator/ConstraintValidator.php

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

0 commit comments

Comments
 (0)
0