diff --git a/src/Symfony/Component/Validator/Constraints/NullValidator.php b/src/Symfony/Component/Validator/Constraints/NullValidator.php index e8fa24ed74..9753f43315 100644 --- a/src/Symfony/Component/Validator/Constraints/NullValidator.php +++ b/src/Symfony/Component/Validator/Constraints/NullValidator.php @@ -27,6 +27,12 @@ class NullValidator extends ConstraintValidator public function validate($value, Constraint $constraint) { if (null !== $value) { + if (is_object($value)) { + $value = get_class($value); + } elseif (is_array($value)) { + $value = 'Array'; + } + $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php index 4466aa17c9..d343c869fd 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php @@ -43,7 +43,7 @@ class NullValidatorTest extends \PHPUnit_Framework_TestCase /** * @dataProvider getInvalidValues */ - public function testInvalidValues($value) + public function testInvalidValues($value, $readableValue) { $constraint = new Null(array( 'message' => 'myMessage' @@ -52,7 +52,7 @@ class NullValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $value, + '{{ value }}' => $readableValue, )); $this->validator->validate($value, $constraint); @@ -61,10 +61,13 @@ class NullValidatorTest extends \PHPUnit_Framework_TestCase public function getInvalidValues() { return array( - array(0), - array(false), - array(true), - array(''), + array(0, 0), + array(false, false), + array(true, true), + array('', ''), + array('foo bar', 'foo bar'), + array(new \DateTime(), 'DateTime'), + array(array(), 'Array'), ); } }