From 187fc1c538b2f44ddc3d035e51f2755b66312226 Mon Sep 17 00:00:00 2001 From: Ahmed Eben Hassine Date: Mon, 2 Nov 2020 23:57:17 +0100 Subject: [PATCH 1/4] feat: improve unique entity violations params --- .../Validator/Constraints/UniqueEntityValidator.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php b/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php index cbf40af2b5750..7c5d259b45fc7 100644 --- a/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php +++ b/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php @@ -167,13 +167,19 @@ public function validate($entity, Constraint $constraint) $errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0]; $invalidValue = isset($criteria[$errorPath]) ? $criteria[$errorPath] : $criteria[$fields[0]]; - $this->context->buildViolation($constraint->message) + $constraintViolationBuilder = $this->context->buildViolation($constraint->message) ->atPath($errorPath) ->setParameter('{{ value }}', $this->formatWithIdentifiers($em, $class, $invalidValue)) ->setInvalidValue($invalidValue) ->setCode(UniqueEntity::NOT_UNIQUE_ERROR) ->setCause($result) - ->addViolation(); + ; + foreach ($fields as $field) { + $invalidValue = ($criteria[$field]) ? $this->formatWithIdentifiers($em, $class, $criteria[$field]) : ""; + $constraintViolationBuilder->setParameter(\sprintf('{{ %s }}', $field), $invalidValue); + } + + $constraintViolationBuilder->addViolation(); } private function formatWithIdentifiers($em, $class, $value) From 995d080f2728a05bfad4c01fcc7576074b2fe3f8 Mon Sep 17 00:00:00 2001 From: Ahmed Eben Hassine Date: Mon, 2 Nov 2020 23:58:04 +0100 Subject: [PATCH 2/4] test: update tests --- .../Constraints/UniqueEntityValidatorTest.php | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php b/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php index 985d4601bf4d5..42220b8cf1dbc 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php @@ -197,6 +197,7 @@ public function testValidateUniqueness() $this->buildViolation('myMessage') ->atPath('property.path.name') ->setParameter('{{ value }}', '"Foo"') + ->setParameter('{{ name }}', '"Foo"') ->setInvalidValue($entity2) ->setCause([$entity1]) ->setCode(UniqueEntity::NOT_UNIQUE_ERROR) @@ -223,6 +224,7 @@ public function testValidateCustomErrorPath() $this->buildViolation('myMessage') ->atPath('property.path.bar') ->setParameter('{{ value }}', '"Foo"') + ->setParameter('{{ name }}', '"Foo"') ->setInvalidValue($entity2) ->setCause([$entity1]) ->setCode(UniqueEntity::NOT_UNIQUE_ERROR) @@ -277,6 +279,8 @@ public function testValidateUniquenessWithIgnoreNullDisabled() $this->buildViolation('myMessage') ->atPath('property.path.name') ->setParameter('{{ value }}', '"Foo"') + ->setParameter('{{ name }}', '"Foo"') + ->setParameter('{{ name2 }}', '') ->setInvalidValue('Foo') ->setCause([$entity1]) ->setCode(UniqueEntity::NOT_UNIQUE_ERROR) @@ -354,6 +358,8 @@ public function testValidateUniquenessWithValidCustomErrorPath() $this->buildViolation('myMessage') ->atPath('property.path.name2') ->setParameter('{{ value }}', '"Bar"') + ->setParameter('{{ name }}', '"Foo"') + ->setParameter('{{ name2 }}', '"Bar"') ->setInvalidValue('Bar') ->setCause([$entity1]) ->setCode(UniqueEntity::NOT_UNIQUE_ERROR) @@ -489,6 +495,7 @@ public function testAssociatedEntity() $this->buildViolation('myMessage') ->atPath('property.path.single') ->setParameter('{{ value }}', 'foo') + ->setParameter('{{ single }}', 'foo') ->setInvalidValue($entity1) ->setCode(UniqueEntity::NOT_UNIQUE_ERROR) ->setCause([$associated, $associated2]) @@ -527,6 +534,7 @@ public function testValidateUniquenessNotToStringEntityWithAssociatedEntity() $this->buildViolation('myMessage') ->atPath('property.path.single') ->setParameter('{{ value }}', $expectedValue) + ->setParameter('{{ single }}', $expectedValue) ->setInvalidValue($entity1) ->setCause([$associated, $associated2]) ->setCode(UniqueEntity::NOT_UNIQUE_ERROR) @@ -586,6 +594,7 @@ public function testValidateUniquenessWithArrayValue() $this->buildViolation('myMessage') ->atPath('property.path.phoneNumbers') ->setParameter('{{ value }}', 'array') + ->setParameter('{{ phoneNumbers }}', 'array') ->setInvalidValue([123]) ->setCause([$entity1]) ->setCode(UniqueEntity::NOT_UNIQUE_ERROR) @@ -690,7 +699,7 @@ public function testValidateInheritanceUniqueness() ->setInvalidValue('Foo') ->setCode('23bd9dbf-6b9b-41cd-a99e-4844bcf3077f') ->setCause([$entity1]) - ->setParameters(['{{ value }}' => '"Foo"']) + ->setParameters(['{{ value }}' => '"Foo"', '{{ name }}' => '"Foo"']) ->assertRaised(); } @@ -733,11 +742,14 @@ public function testValidateUniquenessWithCompositeObjectNoToStringIdEntity() $this->validator->validate($newEntity, $constraint); - $expectedValue = 'object("Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity") identified by (id => 1)'; + $expectedValueForObjectOne = 'object("Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity") identified by (id => 1)'; + $expectedValueForObjectTwo = 'object("Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity") identified by (id => 2)'; $this->buildViolation('myMessage') ->atPath('property.path.objectOne') - ->setParameter('{{ value }}', $expectedValue) + ->setParameter('{{ value }}', $expectedValueForObjectOne) + ->setParameter('{{ objectOne }}', $expectedValueForObjectOne) + ->setParameter('{{ objectTwo }}', $expectedValueForObjectTwo) ->setInvalidValue($objectOne) ->setCause([$entity]) ->setCode(UniqueEntity::NOT_UNIQUE_ERROR) @@ -766,6 +778,7 @@ public function testValidateUniquenessWithCustomDoctrineTypeValue() $this->buildViolation('myMessage') ->atPath('property.path.name') ->setParameter('{{ value }}', $expectedValue) + ->setParameter('{{ name }}', $expectedValue) ->setInvalidValue($existingEntity->name) ->setCause([$existingEntity]) ->setCode(UniqueEntity::NOT_UNIQUE_ERROR) @@ -802,6 +815,7 @@ public function testValidateUniquenessCause() $this->buildViolation('myMessage') ->atPath('property.path.name') ->setParameter('{{ value }}', '"Foo"') + ->setParameter('{{ name }}', '"Foo"') ->setInvalidValue($entity2) ->setCause([$entity1]) ->setCode(UniqueEntity::NOT_UNIQUE_ERROR) From 8a2da1c7aad82e2409c48e04edd403058abd02d7 Mon Sep 17 00:00:00 2001 From: Ahmed Eben Hassine Date: Tue, 3 Nov 2020 09:45:37 +0100 Subject: [PATCH 3/4] ci: fix fabopt.io build --- .../Doctrine/Validator/Constraints/UniqueEntityValidator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php b/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php index 7c5d259b45fc7..8a0e566837d06 100644 --- a/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php +++ b/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php @@ -175,8 +175,8 @@ public function validate($entity, Constraint $constraint) ->setCause($result) ; foreach ($fields as $field) { - $invalidValue = ($criteria[$field]) ? $this->formatWithIdentifiers($em, $class, $criteria[$field]) : ""; - $constraintViolationBuilder->setParameter(\sprintf('{{ %s }}', $field), $invalidValue); + $invalidValue = ($criteria[$field]) ? $this->formatWithIdentifiers($em, $class, $criteria[$field]) : ''; + $constraintViolationBuilder->setParameter(sprintf('{{ %s }}', $field), $invalidValue); } $constraintViolationBuilder->addViolation(); From 263dc8f960b00651a4f7cb3ae0aff00dba2d4c98 Mon Sep 17 00:00:00 2001 From: Ahmed EBEN HASSINE Date: Sat, 21 Nov 2020 01:04:44 +0100 Subject: [PATCH 4/4] Update UniqueEntityValidator.php --- .../Validator/Constraints/UniqueEntityValidator.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php b/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php index 8a0e566837d06..560c2c6e0fb6e 100644 --- a/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php +++ b/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php @@ -175,8 +175,10 @@ public function validate($entity, Constraint $constraint) ->setCause($result) ; foreach ($fields as $field) { - $invalidValue = ($criteria[$field]) ? $this->formatWithIdentifiers($em, $class, $criteria[$field]) : ''; - $constraintViolationBuilder->setParameter(sprintf('{{ %s }}', $field), $invalidValue); + $constraintViolationBuilder->setParameter( + sprintf('{{ %s }}', $field), + ($criteria[$field]) ? $this->formatWithIdentifiers($em, $class, $criteria[$field]) : '' + ); } $constraintViolationBuilder->addViolation();