Skip to content

Commit 9095b3c

Browse files
author
Dawid Sajdak
committed
Unique Entity Validator Invalid Value
1 parent c4a3991 commit 9095b3c

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,37 @@ public function testValidateUniquenessWithIgnoreNull()
238238
->assertRaised();
239239
}
240240

241+
public function testValidateUniquenessWithValidCustomErrorPath()
242+
{
243+
$constraint = new UniqueEntity(array(
244+
'message' => 'myMessage',
245+
'fields' => array('name', 'name2'),
246+
'em' => self::EM_NAME,
247+
'errorPath' => "name2",
248+
));
249+
250+
$entity1 = new DoubleNameEntity(1, 'Foo', "Bar");
251+
$entity2 = new DoubleNameEntity(2, 'Foo', "Bar");
252+
253+
$this->validator->validate($entity1, $constraint);
254+
255+
$this->assertNoViolation();
256+
257+
$this->em->persist($entity1);
258+
$this->em->flush();
259+
260+
$this->validator->validate($entity1, $constraint);
261+
262+
$this->assertNoViolation();
263+
264+
$this->validator->validate($entity2, $constraint);
265+
266+
$this->buildViolation('myMessage')
267+
->atPath('property.path.name2')
268+
->setInvalidValue('Bar')
269+
->assertRaised();
270+
}
271+
241272
public function testValidateUniquenessUsingCustomRepositoryMethod()
242273
{
243274
$constraint = new UniqueEntity(array(

Validator/Constraints/UniqueEntityValidator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ public function validate($entity, Constraint $constraint)
128128
}
129129

130130
$errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];
131+
$invalidValue = isset($criteria[$errorPath]) ? $criteria[$errorPath] : $criteria[$fields[0]];
131132

132-
$this->context->addViolationAt($errorPath, $constraint->message, array(), $criteria[$fields[0]]);
133+
$this->context->addViolationAt($errorPath, $constraint->message, array(), $invalidValue);
133134
}
134135
}

0 commit comments

Comments
 (0)