8000 [Validator] Allow `ConstraintViolation::__toString()` to expose codes that are not null or emtpy strings by phansys · Pull Request #29375 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Allow ConstraintViolation::__toString() to expose codes that are not null or emtpy strings #29375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Validator] Allow ConstraintViolation::__toString() to expose codes…
… that are not null or emtpy strings
  • Loading branch information
phansys authored and nicolas-grekas committed Dec 1, 2018
commit 7bb0fb5cc3a60adb50a7d9b1986c1218f2b8525c
4 changes: 2 additions & 2 deletions src/Symfony/Component/Validator/ConstraintViolation.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ public function __toString()
}

$propertyPath = (string) $this->propertyPath;
$code = $this->code;
$code = (string) $this->code;

if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) {
$class .= '.';
}

if (!empty($code)) {
if ('' !== $code) {
$code = ' (code '.$code.')';
}

Expand Down
55 changes: 55 additions & 0 deletions src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,59 @@ public function testToStringHandlesArrayRoots()

$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);
}
}
0