Skip to content

[DoctrineBridge] make sure that null can be the invalid value #21562

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
Feb 12, 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
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand Down Expand Up @@ -132,6 +133,7 @@ private function createSchema(ObjectManager $em)
$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'),
));
Expand Down Expand Up @@ -213,7 +215,7 @@ public function testValidateUniquenessWithNull()
$this->assertNoViolation();
}

public function testValidateUniquenessWithIgnoreNull()
public function testValidateUniquenessWithIgnoreNullDisabled()
{
$constraint = new UniqueEntity(array(
'message' => 'myMessage',
Expand Down Expand Up @@ -261,6 +263,34 @@ public function testAllConfiguredFieldsAreCheckedOfBeingMappedByDoctrineWithIgno
$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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,19 @@ public function validate($entity, Constraint $constraint)
/* @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));
}

$fieldValue = $class->reflFields[$fieldName]->getValue($entity);

if (null === $fieldValue) {
$hasNullValue = true;
}

if ($constraint->ignoreNull && null === $fieldValue) {
continue;
}
Expand All @@ -102,6 +108,11 @@ public function validate($entity, Constraint $constraint)
}
}

// 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)) {
Expand Down