bug #29375 [Validator] Allow ConstraintViolation::__toString()
to expose codes that are not null or emtpy strings (phansys)
This PR was squashed before being merged into the 3.4 branch (closes #29375).
Discussion
----------
[Validator] Allow `ConstraintViolation::__toString()` to expose codes that are not null or emtpy strings
|Q |A |
|--- |---|
|Branch |2.8|
|Bug fix? |yes|
|New feature? |no |
|BC breaks? |no |
|Deprecations?|no |
|Tests pass? |yes|
|Fixed tickets|n/a|
|License |MIT|
|Doc PR |n/a|
Allow to expose `0` or `"0"` validation codes.
Commits
-------
7bb0fb5cc3
[Validator] Allow `ConstraintViolation::__toString()` to expose codes that are not null or emtpy strings
This commit is contained in:
commit
1905cde840
@ -79,13 +79,13 @@ class ConstraintViolation implements ConstraintViolationInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
$propertyPath = (string) $this->propertyPath;
|
$propertyPath = (string) $this->propertyPath;
|
||||||
$code = $this->code;
|
$code = (string) $this->code;
|
||||||
|
|
||||||
if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) {
|
if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) {
|
||||||
$class .= '.';
|
$class .= '.';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($code)) {
|
if ('' !== $code) {
|
||||||
$code = ' (code '.$code.')';
|
$code = ' (code '.$code.')';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,4 +53,59 @@ EOF;
|
|||||||
|
|
||||||
$this->assertSame($expected, (string) $violation);
|
$this->assertSame($expected, (string) $violation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testToStringHandlesCodes()
|
||||||
|
{
|
||||||
|
$violation = new ConstraintViolation(
|
||||||
|
'42 cannot be used here',
|
||||||
|
'this is the message template',
|
||||||
|
array(),
|
||||||
|
array('some_value' => 42),
|
||||||
|
'some_value',
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
|
||||||
|
$expected = <<<'EOF'
|
||||||
|
Array.some_value:
|
||||||
|
42 cannot be used here (code 0)
|
||||||
|
EOF;
|
||||||
|
|
||||||
|
$this->assertSame($expected, (string) $violation);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToStringOmitsEmptyCodes()
|
||||||
|
{
|
||||||
|
$expected = <<<'EOF'
|
||||||
|
Array.some_value:
|
||||||
|
42 cannot be used here
|
||||||
|
EOF;
|
||||||
|
|
||||||
|
$violation = new ConstraintViolation(
|
||||||
|
'42 cannot be used here',
|
||||||
|
'this is the message template',
|
||||||
|
array(),
|
||||||
|
array('some_value' => 42),
|
||||||
|
'some_value',
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertSame($expected, (string) $violation);
|
||||||
|
|
||||||
|
$violation = new ConstraintViolation(
|
||||||
|
'42 cannot be used here',
|
||||||
|
'this is the message template',
|
||||||
|
array(),
|
||||||
|
array('some_value' => 42),
|
||||||
|
'some_value',
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
''
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertSame($expected, (string) $violation);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user