Unique Entity Validator Invalid Value
This commit is contained in:
parent
975b8a8f7d
commit
58bf5822b2
|
@ -238,6 +238,37 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
|
|||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testValidateUniquenessWithValidCustomErrorPath()
|
||||
{
|
||||
$constraint = new UniqueEntity(array(
|
||||
'message' => 'myMessage',
|
||||
'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('myMessage')
|
||||
->atPath('property.path.name2')
|
||||
->setInvalidValue('Bar')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testValidateUniquenessUsingCustomRepositoryMethod()
|
||||
{
|
||||
$constraint = new UniqueEntity(array(
|
||||
|
|
|
@ -128,7 +128,8 @@ class UniqueEntityValidator extends ConstraintValidator
|
|||
}
|
||||
|
||||
$errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];
|
||||
$invalidValue = isset($criteria[$errorPath]) ? $criteria[$errorPath] : $criteria[$fields[0]];
|
||||
|
||||
$this->context->addViolationAt($errorPath, $constraint->message, array(), $criteria[$fields[0]]);
|
||||
$this->context->addViolationAt($errorPath, $constraint->message, array(), $invalidValue);
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue