bug #21562 [DoctrineBridge] make sure that null can be the invalid value (xabbuh)
This PR was merged into the 2.7 branch.
Discussion
----------
[DoctrineBridge] make sure that null can be the invalid value
| Q | A
| ------------- | ---
| Branch? | 2.7
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #21554
| License | MIT
| Doc PR |
Commits
-------
c3702c2d8f
make sure that null can be the invalid value
This commit is contained in:
commit
e7541d9552
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
|
||||
|
||||
use Doctrine\ORM\Mapping\Id;
|
||||
use Doctrine\ORM\Mapping\Column;
|
||||
use Doctrine\ORM\Mapping\Entity;
|
||||
|
||||
/** @Entity */
|
||||
class DoubleNullableNameEntity
|
||||
{
|
||||
/** @Id @Column(type="integer") */
|
||||
protected $id;
|
||||
|
||||
/** @Column(type="string", nullable=true) */
|
||||
public $name;
|
||||
|
||||
/** @Column(type="string", nullable=true) */
|
||||
public $name2;
|
||||
|
||||
public function __construct($id, $name, $name2)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->name2 = $name2;
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@ use Doctrine\Common\Persistence\ObjectRepository;
|
||||
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
|
||||
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
|
||||
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity;
|
||||
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNullableNameEntity;
|
||||
use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity;
|
||||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
|
||||
@ -132,6 +133,7 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
|
||||
$schemaTool->createSchema(array(
|
||||
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'),
|
||||
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity'),
|
||||
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNullableNameEntity'),
|
||||
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'),
|
||||
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity'),
|
||||
));
|
||||
@ -213,7 +215,7 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testValidateUniquenessWithIgnoreNull()
|
||||
public function testValidateUniquenessWithIgnoreNullDisabled()
|
||||
{
|
||||
$constraint = new UniqueEntity(array(
|
||||
'message' => 'myMessage',
|
||||
@ -261,6 +263,34 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
|
||||
$this->validator->validate($entity1, $constraint);
|
||||
}
|
||||
|
||||
public function testNoValidationIfFirstFieldIsNullAndNullValuesAreIgnored()
|
||||
{
|
||||
$constraint = new UniqueEntity(array(
|
||||
'message' => 'myMessage',
|
||||
'fields' => array('name', 'name2'),
|
||||
'em' => self::EM_NAME,
|
||||
'ignoreNull' => true,
|
||||
));
|
||||
|
||||
$entity1 = new DoubleNullableNameEntity(1, null, 'Foo');
|
||||
$entity2 = new DoubleNullableNameEntity(2, null, 'Foo');
|
||||
|
||||
$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->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testValidateUniquenessWithValidCustomErrorPath()
|
||||
{
|
||||
$constraint = new UniqueEntity(array(
|
||||
|
@ -80,6 +80,8 @@ class UniqueEntityValidator extends ConstraintValidator
|
||||
/* @var $class \Doctrine\Common\Persistence\Mapping\ClassMetadata */
|
||||
|
||||
$criteria = array();
|
||||
$hasNullValue = false;
|
||||
|
||||
foreach ($fields as $fieldName) {
|
||||
if (!$class->hasField($fieldName) && !$class->hasAssociation($fieldName)) {
|
||||
throw new ConstraintDefinitionException(sprintf('The field "%s" is not mapped by Doctrine, so it cannot be validated for uniqueness.', $fieldName));
|
||||
@ -87,6 +89,10 @@ class UniqueEntityValidator extends ConstraintValidator
|
||||
|
||||
$fieldValue = $class->reflFields[$fieldName]->getValue($entity);
|
||||
|
||||
if (null === $fieldValue) {
|
||||
$hasNullValue = true;
|
||||
}
|
||||
|
||||
if ($constraint->ignoreNull && null === $fieldValue) {
|
||||
continue;
|
||||
}
|
||||
@ -102,6 +108,11 @@ class UniqueEntityValidator extends ConstraintValidator
|
||||
}
|
||||
}
|
||||
|
||||
// validation doesn't fail if one of the fields is null and if null values should be ignored
|
||||
if ($hasNullValue && $constraint->ignoreNull) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 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)) {
|
||||
|
Reference in New Issue
Block a user