bug #28252 [DoctrineBridge] support __toString as documented for UniqueEntityValidator (dmaicher)

This PR was squashed before being merged into the 3.4 branch (closes #28252).

Discussion
----------

[DoctrineBridge] support __toString as documented for UniqueEntityValidator

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #28245
| License       | MIT
| Doc PR        | -

This fixes https://github.com/symfony/symfony/issues/28245.

It brings back handling `__toString` as documented for invalid values.

Commits
-------

2ac883a99b [DoctrineBridge] support __toString as documented for UniqueEntityValidator
This commit is contained in:
Nicolas Grekas 2018-08-24 11:45:48 +02:00
commit d446b6aefb
2 changed files with 5 additions and 1 deletions

View File

@ -482,7 +482,7 @@ class UniqueEntityValidatorTest extends ConstraintValidatorTestCase
$this->buildViolation('myMessage')
->atPath('property.path.single')
->setParameter('{{ value }}', 'object("Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity") identified by (id => 1)')
->setParameter('{{ value }}', 'foo')
->setInvalidValue($entity1)
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->setCause(array($associated, $associated2))

View File

@ -186,6 +186,10 @@ class UniqueEntityValidator extends ConstraintValidator
return $this->formatValue($value, self::PRETTY_DATE);
}
if (\method_exists($value, '__toString')) {
return (string) $value;
}
if ($class->getName() !== $idClass = \get_class($value)) {
// non unique value might be a composite PK that consists of other entity objects
if ($em->getMetadataFactory()->hasMetadataFor($idClass)) {