10000 merged branch stof/unique_validator_error_mapping (PR #4868) · jacobmaster/symfony@7680101 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7680101

Browse files
committed
merged branch stof/unique_validator_error_mapping (PR symfony#4868)
Commits ------- ac78755 [DoctrineBridge] Added an option to choose the subpath for the violation Discussion ---------- [DoctrineBridge] Added an option to choose the subpath for the violation Bug fix: no Feature addition: yes Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: symfony#3866 Todo: - By default, the UniqueEntityValidator maps the violation on the first field of the UniqueEntity constraint. The new option allows to control this behavior if a better mapping is suited.
2 parents d4b40fb + ac78755 commit 7680101

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueValidatorTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected function createValidatorFactory($uniqueValidator)
6969
return $validatorFactory;
7070
}
7171

72-
public function createValidator($entityManagerName, $em, $validateClass = null, $uniqueFields = null)
72+
public function createValidator($entityManagerName, $em, $validateClass = null, $uniqueFields = null, $errorPath = null)
7373
{
7474
if (!$validateClass) {
7575
$validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity';
@@ -83,7 +83,7 @@ public function createValidator($entityManagerName, $em, $validateClass = null,
8383
$uniqueValidator = new UniqueEntityValidator($registry);
8484

8585
$metadata = new ClassMetadata($validateClass);
86-
$metadata->addConstraint(new UniqueEntity(array('fields' => $uniqueFields, 'em' => $entityManagerName)));
86+
$metadata->addConstraint(new UniqueEntity(array('fields' => $uniqueFields, 'em' => $entityManagerName, 'errorPath' => $errorPath)));
8787

8888
$metadataFactory = $this->createMetadataFactoryMock($metadata);
8989
$validatorFactory = $this->createValidatorFactory($uniqueValidator);
@@ -132,6 +132,29 @@ public function testValidateUniqueness()
132132
$this->assertEquals('Foo', $violation->getInvalidValue());
133133
}
134134

135+
public function testValidateCustomErrorPath()
136+
{
137+
$entityManagerName = "foo";
138+
$em = $this->createTestEntityManager();
139+
$this->createSchema($em);
140+
$validator = $this->createValidator($entityManagerName, $em, null, null, 'bar');
141+
142+
$entity1 = new SingleIdentEntity(1, 'Foo');
143+
144+
$em->persist($entity1);
145+
$em->flush();
146+
147+
$entity2 = new SingleIdentEntity(2, 'Foo');
148+
149+
$violationsList = $validator->validate($entity2);
150+
$this->assertEquals(1, $violationsList->count(), "No violations found on entity after it was saved to the database.");
151+
152+
$violation = $violationsList[0];
153+
$this->assertEquals('This value is already used.', $violation->getMessage());
154+
$this->assertEquals('bar', $violation->getPropertyPath());
155+
$this->assertEquals('Foo', $violation->getInvalidValue());
156+
}
157+
135158
public function testValidateUniquenessWithNull()
136159
{
137160
$entityManagerName = "foo";

src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntity.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class UniqueEntity extends Constraint
2525
public $service = 'doctrine.orm.validator.unique';
2626
public $em = null;
2727
public $fields = array();
28+
public $errorPath = null;
2829

2930
public function getRequiredOptions()
3031
{

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public function validate($entity, Constraint $constraint)
4747
throw new UnexpectedTypeException($constraint->fields, 'array');
4848
}
4949

50+
if (null !== $constraint->errorPath && !is_string($constraint->errorPath)) {
51+
throw new UnexpectedTypeException($constraint->errorPath, 'string or null');
52+
}
53+
5054
$fields = (array) $constraint->fields;
5155

5256
if (0 === count($fields)) {
@@ -114,6 +118,8 @@ public function validate($entity, Constraint $constraint)
114118
return;
115119
}
116120

117-
$this->context->addViolationAtSubPath($fields[0], $constraint->message, array(), $criteria[$fields[0]]);
121+
$errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];
122+
123+
$this->context->addViolationAtSubPath($errorPath, $constraint->message, array(), $criteria[$fields[0]]);
118124
}
119125
}

0 commit comments

Comments
 (0)
0