From c53278a757e5e75c36767e020ea176bb116793c7 Mon Sep 17 00:00:00 2001 From: Jamal Youssefi Date: Tue, 14 Jun 2016 10:07:50 +0200 Subject: [PATCH] Add custom message for unique entity constraint --- .../Constraints/UniqueEntityValidatorTest.php | 63 +++++++++++++++++++ .../Constraints/UniqueEntityValidator.php | 30 +++++---- src/Symfony/Bridge/Doctrine/composer.json | 3 +- 3 files changed, 84 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php b/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php index 33e484320a56a..e4fa353df7878 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php @@ -470,4 +470,67 @@ public function testEntityManagerNullObject() $this->validator->validate($entity, $constraint); } + + public function testCustomMessageWithOneUniqueField() + { + $constraint = new UniqueEntity(array( + 'message' => 'An entity with name "{{ name }}" already exists.', + 'fields' => array('name'), + 'em' => self::EM_NAME, + )); + + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Foo'); + + $this->validator->validate($entity1, $constraint); + + $this->assertNoViolation(); + + $this->em->persist($entity1); + $this->em->flush(); + + $this->validator->validate($entity1, $constraint); + + $this->assertNoViolation(); + + $this->validator->validate($entity2, $constraint); + + $this->buildViolation('An entity with name "{{ name }}" already exists.') + ->setParameters(array('{{ name }}' => 'Foo')) + ->atPath('property.path.name') + ->setInvalidValue('Foo') + ->assertRaised(); + } + + public function testCustomMessageWithTwoUniqueFields() + { + $constraint = new UniqueEntity(array( + 'message' => 'An entity with name1 "{{ name }}" and name2 "{{ name2 }}" already exists.', + 'fields' => array('name', 'name2'), + 'em' => self::EM_NAME, + 'errorPath' => 'name2', + )); + + $entity1 = new DoubleNameEntity(1, 'Foo', 'Bar'); + $entity2 = new DoubleNameEntity(2, 'Foo', 'Bar'); + + $this->validator->validate($entity1, $constraint); + + $this->assertNoViolation(); + + $this->em->persist($entity1); + $this->em->flush(); + + $this->validator->validate($entity1, $constraint); + + $this->assertNoViolation(); + + $this->validator->validate($entity2, $constraint); + + $this->buildViolation('An entity with name1 "{{ name }}" and name2 "{{ name2 }}" already exists.') + ->setParameters(array('{{ name }}' => 'Foo', '{{ name2 }}' => 'Bar')) + ->atPath('property.path.name2') + ->setInvalidValue('Bar') + ->assertRaised(); + } } diff --git a/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php b/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php index f4c8671abae9a..056c73c534a50 100644 --- a/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php +++ b/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php @@ -12,11 +12,11 @@ namespace Symfony\Bridge\Doctrine\Validator\Constraints; use Doctrine\Common\Persistence\ManagerRegistry; -use Symfony\Component\Validator\Context\ExecutionContextInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\UnexpectedTypeException; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; use Symfony\Component\Validator\ConstraintValidator; +use Symfony\Component\PropertyAccess\PropertyAccess; /** * Unique Entity Validator checks if one or a set of fields contain unique values. @@ -128,16 +128,24 @@ public function validate($entity, Constraint $constraint) $errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0]; $invalidValue = isset($criteria[$errorPath]) ? $criteria[$errorPath] : $criteria[$fields[0]]; - if ($this->context instanceof ExecutionContextInterface) { - $this->context->buildViolation($constraint->message) - ->atPath($errorPath) - ->setInvalidValue($invalidValue) - ->addViolation(); - } else { - $this->buildViolation($constraint->message) - ->atPath($errorPath) - ->setInvalidValue($invalidValue) - ->addViolation(); + $parameters = array(); + + if (preg_match_all('/{{ ([a-zA-Z0-9_]+) }}/', $constraint->message, $vars) > 0) { + + if (!class_exists('Symfony\\Component\\PropertyAccess\\PropertyAccess')) { + throw new \RuntimeException('Unable to access entity property as the Symfony PropertyAccess is not installed.'); + } + + $accessor = PropertyAccess::createPropertyAccessor(); + + foreach ($vars[1] as $var) { + $parameters[sprintf('{{ %s }}', $var)] = $accessor->getValue($entity, $var); + } } + + $this->context->buildViolation($constraint->message, $parameters) + ->atPath($errorPath) + ->setInvalidValue($invalidValue) + ->addViolation(); } } diff --git a/src/Symfony/Bridge/Doctrine/composer.json b/src/Symfony/Bridge/Doctrine/composer.json index f0f79f522bbd1..fcc4e6143bbf4 100644 --- a/src/Symfony/Bridge/Doctrine/composer.json +++ b/src/Symfony/Bridge/Doctrine/composer.json @@ -41,7 +41,8 @@ "symfony/property-info": "", "doctrine/data-fixtures": "", "doctrine/dbal": "", - "doctrine/orm": "" + "doctrine/orm": "", + "symfony/property-access": "" }, "autoload": { "psr-4": { "Symfony\\Bridge\\Doctrine\\": "" }, pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy