Skip to content

[DoctrineBridge] always check for all fields to be mapped #21431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,23 @@ public function testValidateUniquenessWithIgnoreNull()
->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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ public function validate($entity, Constraint $constraint)
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
Expand All @@ -100,6 +102,12 @@ public function validate($entity, Constraint $constraint)
}
}

// 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);

Expand Down