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

Skip to content

Commit 2cec67e

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

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
13+
14+
use Doctrine\ORM\Mapping\Id;
15+
use Doctrine\ORM\Mapping\Column;
16+
use Doctrine\ORM\Mapping\Entity;
17+
18+
/** @Entity */
19+
class DoubleNullableNameEntity
20+
{
21+
/** @Id @Column(type="integer") */
22+
protected $id;
23+
24+
/** @Column(type="string", nullable=true) */
25+
public $name;
26+
27+
/** @Column(type="string", nullable=true) */
28+
public $name2;
29+
30+
public function __construct($id, $name, $name2)
31+
{
32+
$this->id = $id;
33+
$this->name = $name;
34+
$this->name2 = $name2;
35+
}
36+
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
1919
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
2020
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity;
21+
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNullableNameEntity;
2122
use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity;
2223
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
2324
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
@@ -132,6 +133,7 @@ private function createSchema(ObjectManager $em)
132133
$schemaTool->createSchema(array(
133134
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'),
134135
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity'),
136+
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNullableNameEntity'),
135137
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'),
136138
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity'),
137139
));
@@ -261,6 +263,37 @@ public function testAllConfiguredFieldsAreCheckedOfBeingMappedByDoctrineWithIgno
261263
$this->validator->validate($entity1, $constraint);
262264
}
263265

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