Fixes message value for objects
This commit is contained in:
parent
2997baae24
commit
78eeba8f01
@ -27,6 +27,12 @@ class NullValidator extends ConstraintValidator
|
|||||||
public function validate($value, Constraint $constraint)
|
public function validate($value, Constraint $constraint)
|
||||||
{
|
{
|
||||||
if (null !== $value) {
|
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));
|
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ class NullValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
/**
|
/**
|
||||||
* @dataProvider getInvalidValues
|
* @dataProvider getInvalidValues
|
||||||
*/
|
*/
|
||||||
public function testInvalidValues($value)
|
public function testInvalidValues($value, $readableValue)
|
||||||
{
|
{
|
||||||
$constraint = new Null(array(
|
$constraint = new Null(array(
|
||||||
'message' => 'myMessage'
|
'message' => 'myMessage'
|
||||||
@ -52,7 +52,7 @@ class NullValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->context->expects($this->once())
|
$this->context->expects($this->once())
|
||||||
->method('addViolation')
|
->method('addViolation')
|
||||||
->with('myMessage', array(
|
->with('myMessage', array(
|
||||||
'{{ value }}' => $value,
|
'{{ value }}' => $readableValue,
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->validator->validate($value, $constraint);
|
$this->validator->validate($value, $constraint);
|
||||||
@ -61,10 +61,13 @@ class NullValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function getInvalidValues()
|
public function getInvalidValues()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
array(0),
|
array(0, 0),
|
||||||
array(false),
|
array(false, false),
|
||||||
array(true),
|
array(true, true),
|
||||||
array(''),
|
array('', ''),
|
||||||
|
array('foo bar', 'foo bar'),
|
||||||
|
array(new \DateTime(), 'DateTime'),
|
||||||
|
array(array(), 'Array'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user