Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] Fixed constraint violation to string not handling array #10007

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion src/Symfony/Component/Validator/ConstraintViolation.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ public function __construct($message, $messageTemplate, array $messageParameters
*/
public function __toString()
{
$class = (string) (is_object($this->root) ? get_class($this->root) : $this->root);
$class = is_object($this->root)
? get_class($this->root)
: (is_array($this->root) ? 'Array' : (string) $this->root);

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

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

$this->assertSame($expected, (string) $violation);
}

public function testToStringHandlesArraysRootAsValue()
{
$violation = new ConstraintViolation(
'Array',
'{{ value }}',
array('{{ value }}' => array(1, 2, 3)),
array(),
'property.path',
null
);

$expected = <<<EOF
Array.property.path:
Array
EOF;

$this->assertSame($expected, (string) $violation);
}
}
11 changes: 11 additions & 0 deletions src/Symfony/Component/Validator/Tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Validator\ConstraintValidatorFactory;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\Type;

class ValidatorTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -232,6 +233,16 @@ public function testValidateValueRejectsValid()
$this->validator->validateValue($entity, new Valid());
}

public function testValidateValueViolationsToStringHandlesArrays()
{
$violations = $this->validator->validateValue(
array(),
array(new Type(array('type' => 'string')))
);

$this->assertInternalType('string', (string) $violations);
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ValidatorException
*/
Expand Down