Skip to content
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
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);
}
}