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

Skip to content

Commit 22caebc

Browse files
committed
bug #10123 handle array root element (greg0ire)
This PR was submitted for the 2.4 branch but it was merged into the 2.3 branch instead (closes #10123). Discussion ---------- handle array root element An array to string conversion notice was thrown when the root element of the thing being validated is an array. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT Commits ------- 17ed8bf handle array root element
2 parents ca5eea5 + 17ed8bf commit 22caebc

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Symfony/Component/Validator/ConstraintViolation.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,14 @@ public function __construct($message, $messageTemplate, array $messageParameters
9595
*/
9696
public function __toString()
9797
{
98-
$class = (string) (is_object($this->root) ? get_class($this->root) : $this->root);
98+
if (is_object($this->root)) {
99+
$class = get_class($this->root);
100+
} elseif (is_array($this->root)) {
101+
$class = "Array";
102+
} else {
103+
$class = (string) $this->root;
104+
}
105+
99106
$propertyPath = (string) $this->propertyPath;
100107
$code = $this->code;
101108

src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,23 @@ public function testToStringHandlesArrays()
3333

3434
$this->assertSame($expected, (string) $violation);
3535
}
36+
37+
public function testToStringHandlesArrayRoots()
38+
{
39+
$violation = new ConstraintViolation(
40+
'42 cannot be used here',
41+
'this is the message template',
42+
array(),
43+
array('some_value' => 42),
44+
'some_value',
45+
null
46+
);
47+
48+
$expected = <<<EOF
49+
Array.some_value:
50+
42 cannot be used here
51+
EOF;
52+
53+
$this->assertSame($expected, (string) $violation);
54+
}
3655
}

0 commit comments

Comments
 (0)