8000 bug #9865 [Validator] Fixes message value for objects (jongotlin) · symfony/symfony@c9dd348 · GitHub
[go: up one dir, main page]

Skip to content

Commit c9dd348

Browse files
committed
bug #9865 [Validator] Fixes message value for objects (jongotlin)
This PR was submitted for the 2.2 branch but it was merged into the 2.3 branch instead (closes #9865). Discussion ---------- [Validator] Fixes message value for objects | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 7b69e9b [Validator] Fixes message value for objects
2 parents 2997baa + 78eeba8 commit c9dd348

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/Symfony/Component/Validator/Constraints/NullValidator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class NullValidator extends ConstraintValidator
2727
public function validate($value, Constraint $constraint)
2828
{
2929
if (null !== $value) {
30+
if (is_object($value)) {
31+
$value = get_class($value);
32+
} elseif (is_array($value)) {
33+
$value = 'Array';
34+
}
35+
3036
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
3137
}
3238
}

src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testNullIsValid()
4343
/**
4444
* @dataProvider getInvalidValues
4545
*/
46-
public function testInvalidValues($value)
46+
public function testInvalidValues($value, $readableValue)
4747
{
4848
$constraint = new Null(array(
4949
'message' => 'myMessage'
@@ -52,7 +52,7 @@ public function testInvalidValues($value)
5252
$this->context->expects($this->once())
5353
->method('addViolation')
5454
->with('myMessage', array(
55-
'{{ value }}' => $value,
55+
'{{ value }}' => $readableValue,
5656
));
5757

5858
$this->validator->validate($value, $constraint);
@@ -61,10 +61,13 @@ public function testInvalidValues($value)
6161
public function getInvalidValues()
6262
{
6363
return array(
64-
array(0),
65-
array(false),
66-
array(true),
67-
array(''),
64+
array(0, 0),
65+
array(false, false),
66+
array(true, true),
67+
array('', ''),
68+
array('foo bar', 'foo bar'),
69+
array(new \DateTime(), 'DateTime'),
70+
array(array(), 'Array'),
6871
);
6972
}
7073
}

0 commit comments

Comments
 (0)
0