Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29c553d

Browse files
committed
make sure that null can be the invalid value
1 parent d3673a8 commit 29c553d

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoubleNameEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class DoubleNameEntity
2121
/** @Id @Column(type="integer") */
2222
protected $id;
2323

24-
/** @Column(type="string") */
24+
/** @Column(type="string", nullable=true) */
2525
public $name;
2626

2727
/** @Column(type="string", nullable=true) */

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,37 @@ public function testAllConfiguredFieldsAreCheckedOfBeingMappedByDoctrineWithIgno
261261
$this->validator->validate($entity1, $constraint);
262262
}
263263

264+
public function testErrorIsMappedToFirstFieldEvenIfItIsNullAndNullValuesAreIgnored()
265+
{
266+
$constraint = new UniqueEntity(array(
267+
'message' => 'myMessage',
268+
'fields' => array('name', 'name2'),
269+
'em' => self::EM_NAME,
270+
'ignoreNull' => true,
271+
));
272+
273+
$entity1 = new DoubleNameEntity(1, null, 'Foo');
274+
$entity2 = new DoubleNameEntity(2, null, 'Foo');
275+
276+
$this->validator->validate($entity1, $constraint);
277+
278+
$this->assertNoViolation();
279+
280+
$this->em->persist($entity1);
281+
$this->em->flush();
282+
283+
$this->validator->validate($entity1, $constraint);
284+
285+
$this->assertNoViolation();
286+
287+
$this->validator->validate($entity2, $constraint);
288+
289+
$this->buildViolation('myMessage')
290+
->atPath('property.path.name')
291+
->setInvalidValue(null)
292+
->assertRaised();
293+
}
294+
264295
public function testValidateUniquenessWithValidCustomErrorPath()
265296
{
266297
$constraint = new UniqueEntity(array(

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ public function validate($entity, Constraint $constraint)
8080
/* @var $class \Doctrine\Common\Persistence\Mapping\ClassMetadata */
8181

8282
$criteria = array();
83+
$fieldValues = array();
8384
foreach ($fields as $fieldName) {
8485
if (!$class->hasField($fieldName) && !$class->hasAssociation($fieldName)) {
8586
throw new ConstraintDefinitionException(sprintf('The field "%s" is not mapped by Doctrine, so it cannot be validated for uniqueness.', $fieldName));
8687
}
8788

8889
$fieldValue = $class->reflFields[$fieldName]->getValue($entity);
90+
$fieldValues[$fieldName] = $fieldValue;
8991

9092
if ($constraint->ignoreNull && null === $fieldValue) {
9193
continue;
@@ -134,7 +136,7 @@ public function validate($entity, Constraint $constraint)
134136
}
135137

136138
$errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];
137-
$invalidValue = isset($criteria[$errorPath]) ? $criteria[$errorPath] : $criteria[$fields[0]];
139+
$invalidValue = isset($criteria[$errorPath]) ? $criteria[$errorPath] : $fieldValues[$fields[0]];
138140

139141
if ($this->context instanceof ExecutionContextInterface) {
140142
$this->context->buildViolation($constraint->message)

0 commit comments

Comments
 (0)