Skip to content

Commit

Permalink
handle array root element
Browse files Browse the repository at this point in the history
An array to string conversion notice was thrown when the root element of
the thing being validated is an array.
  • Loading branch information
Grégoire Paris authored and fabpot committed Mar 26, 2014
1 parent ca5eea5 commit 17ed8bf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Symfony/Component/Validator/ConstraintViolation.php
Expand Up @@ -95,7 +95,14 @@ public function __construct($message, $messageTemplate, array $messageParameters
*/
public function __toString()
{
$class = (string) (is_object($this->root) ? get_class($this->root) : $this->root);
if (is_object($this->root)) {
$class = get_class($this->root);
} elseif (is_array($this->root)) {
$class = "Array";
} else {
$class = (string) $this->root;
}

$propertyPath = (string) $this->propertyPath;
$code = $this->code;

Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php
Expand Up @@ -33,4 +33,23 @@ public function testToStringHandlesArrays()

$this->assertSame($expected, (string) $violation);
}

public function testToStringHandlesArrayRoots()
{
$violation = new ConstraintViolation(
'42 cannot be used here',
'this is the message template',
array(),
array('some_value' => 42),
'some_value',
null
);

$expected = <<<EOF
Array.some_value:
42 cannot be used here
EOF;

$this->assertSame($expected, (string) $violation);
}
}

0 comments on commit 17ed8bf

Please sign in to comment.