diff --git a/src/Symfony/Component/Validator/ConstraintViolation.php b/src/Symfony/Component/Validator/ConstraintViolation.php index ca8d525f67..36a42aaec4 100644 --- a/src/Symfony/Component/Validator/ConstraintViolation.php +++ b/src/Symfony/Component/Validator/ConstraintViolation.php @@ -95,7 +95,14 @@ class ConstraintViolation implements ConstraintViolationInterface */ 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; diff --git a/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php b/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php index e1f06c2428..2ceb0169a1 100644 --- a/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php +++ b/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php @@ -33,4 +33,23 @@ EOF; $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 = <<assertSame($expected, (string) $violation); + } }