Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 09dbed7

Browse filesBrowse files
committed
feature #23845 [Validator] Add unique entity violation cause (Ilya Vertakov)
This PR was squashed before being merged into the 3.4 branch (closes #23845). Discussion ---------- [Validator] Add unique entity violation cause | Q | A | ------------- | --- | Branch? | 3.4 or master | Bug fix? | no | New feature? | yes/no <!-- don't forget updating src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | n/a This PR adds unique entity validation error cause which can be array of entities to constraint violation Commits ------- 7ae06dc [Validator] Add unique entity violation cause
2 parents af384ae + 7ae06dc commit 09dbed7
Copy full SHA for 09dbed7

File tree

3 files changed

+48
-0
lines changed
Filter options

3 files changed

+48
-0
lines changed

‎src/Symfony/Bridge/Doctrine/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* added support for doctrine/dbal v2.6 types
8+
* added cause of UniqueEntity constraint violation
89

910
3.1.0
1011
-----

‎src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ public function testValidateUniqueness()
190190
->atPath('property.path.name')
191191
->setParameter('{{ value }}', '"Foo"')
192192
->setInvalidValue($entity2)
193+
->setCause(array($entity1))
193194
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
194195
->assertRaised();
195196
}
@@ -215,6 +216,7 @@ public function testValidateCustomErrorPath()
215216
->atPath('property.path.bar')
216217
->setParameter('{{ value }}', '"Foo"')
217218
->setInvalidValue($entity2)
219+
->setCause(array($entity1))
218220
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
219221
->assertRaised();
220222
}
@@ -268,6 +270,7 @@ public function testValidateUniquenessWithIgnoreNullDisabled()
268270
->atPath('property.path.name')
269271
->setParameter('{{ value }}', '"Foo"')
270272
->setInvalidValue('Foo')
273+
->setCause(array($entity1))
271274
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
272275
->assertRaised();
273276
}
@@ -346,6 +349,7 @@ public function testValidateUniquenessWithValidCustomErrorPath()
346349
->atPath('property.path.name2')
347350
->setParameter('{{ value }}', '"Bar"')
348351
->setInvalidValue('Bar')
352+
->setCause(array($entity1))
349353
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
350354
->assertRaised();
351355
}
@@ -481,6 +485,7 @@ public function testAssociatedEntity()
481485
->setParameter('{{ value }}', 'object("Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity") identified by (id => 1)')
482486
->setInvalidValue($entity1)
483487
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
488+
->setCause(array($associated, $associated2))
484489
->assertRaised();
485490
}
486491

@@ -517,6 +522,7 @@ public function testValidateUniquenessNotToStringEntityWithAssociatedEntity()
517522
->atPath('property.path.single')
518523
->setParameter('{{ value }}', $expectedValue)
519524
->setInvalidValue($entity1)
525+
->setCause(array($associated, $associated2))
520526
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
521527
->assertRaised();
522528
}
@@ -575,6 +581,7 @@ public function testValidateUniquenessWithArrayValue()
575581
->atPath('property.path.phoneNumbers')
576582
->setParameter('{{ value }}', 'array')
577583
->setInvalidValue(array(123))
584+
->setCause(array($entity1))
578585
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
579586
->assertRaised();
580587
}
@@ -652,6 +659,7 @@ public function testValidateInheritanceUniqueness()
652659
->atPath('property.path.name')
653660
->setInvalidValue('Foo')
654661
->setCode('23bd9dbf-6b9b-41cd-a99e-4844bcf3077f')
662+
->setCause(array($entity1))
655663
->setParameters(array('{{ value }}' => '"Foo"'))
656664
->assertRaised();
657665
}
@@ -703,6 +711,7 @@ public function testValidateUniquenessWithCompositeObjectNoToStringIdEntity()
703711
->atPath('property.path.objectOne')
704712
->setParameter('{{ value }}', $expectedValue)
705713
->setInvalidValue($objectOne)
714+
->setCause(array($entity))
706715
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
707716
->assertRaised();
708717
}
@@ -730,6 +739,43 @@ public function testValidateUniquenessWithCustomDoctrineTypeValue()
730739
->atPath('property.path.name')
731740
->setParameter('{{ value }}', $expectedValue)
732741
->setInvalidValue($existingEntity->name)
742+
->setCause(array($existingEntity))
743+
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
744+
->assertRaised();
745+
}
746+
747+
/**
748+
* This is a functional test as there is a large integration necessary to get the validator working.
749+
*/
750+
public function testValidateUniquenessCause()
751+
{
752+
$constraint = new UniqueEntity(array(
753+
'message' => 'myMessage',
754+
'fields' => array('name'),
755+
'em' => self::EM_NAME,
756+
));
757+
758+
$entity1 = new SingleIntIdEntity(1, 'Foo');
759+
$entity2 = new SingleIntIdEntity(2, 'Foo');
760+
761+
$this->validator->validate($entity1, $constraint);
762+
763+
$this->assertNoViolation();
764+
765+
$this->em->persist($entity1);
766+
$this->em->flush();
767+
768+
$this->validator->validate($entity1, $constraint);
769+
770+
$this->assertNoViolation();
771+
772+
$this->validator->validate($entity2, $constraint);
773+
774+
$this->buildViolation('myMessage')
775+
->atPath('property.path.name')
776+
->setParameter('{{ value }}', '"Foo"')
777+
->setInvalidValue($entity2)
778+
->setCause(array($entity1))
733779
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
734780
->assertRaised();
735781
}

‎src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ public function validate($entity, Constraint $constraint)
171171
->setParameter('{{ value }}', $this->formatWithIdentifiers($em, $class, $invalidValue))
172172
->setInvalidValue($invalidValue)
173173
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
174+
->setCause($result)
174175
->addViolation();
175176
}
176177

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.