always check for all fields to be mapped

This commit is contained in:
Christian Flothmann 2017-01-27 10:10:43 +01:00
parent a6d2420f00
commit 1e3421d6f0
2 changed files with 28 additions and 3 deletions

View File

@ -244,6 +244,23 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
->assertRaised();
}
/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testAllConfiguredFieldsAreCheckedOfBeingMappedByDoctrineWithIgnoreNullEnabled()
{
$constraint = new UniqueEntity(array(
'message' => 'myMessage',
'fields' => array('name', 'name2'),
'em' => self::EM_NAME,
'ignoreNull' => true,
));
$entity1 = new SingleIntIdEntity(1, null);
$this->validator->validate($entity1, $constraint);
}
public function testValidateUniquenessWithValidCustomErrorPath()
{
$constraint = new UniqueEntity(array(

View File

@ -85,12 +85,14 @@ class UniqueEntityValidator extends ConstraintValidator
throw new ConstraintDefinitionException(sprintf('The field "%s" is not mapped by Doctrine, so it cannot be validated for uniqueness.', $fieldName));
}
$criteria[$fieldName] = $class->reflFields[$fieldName]->getValue($entity);
$fieldValue = $class->reflFields[$fieldName]->getValue($entity);
if ($constraint->ignoreNull && null === $criteria[$fieldName]) {
return;
if ($constraint->ignoreNull && null === $fieldValue) {
continue;
}
$criteria[$fieldName] = $fieldValue;
if (null !== $criteria[$fieldName] && $class->hasAssociation($fieldName)) {
/* Ensure the Proxy is initialized before using reflection to
* read its identifiers. This is necessary because the wrapped
@ -100,6 +102,12 @@ class UniqueEntityValidator extends ConstraintValidator
}
}
// skip validation if there are no criteria (this can happen when the
// "ignoreNull" option is enabled and fields to be checked are null
if (empty($criteria)) {
return;
}
$repository = $em->getRepository(get_class($entity));
$result = $repository->{$constraint->repositoryMethod}($criteria);