Skip to content

[DoctrineBridge] Added support for boolean value in UniqueEntity #28601

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

Closed
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
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/Doctrine/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.3.0
-----

* added support for `boolean` field type in `UniqueEntityValidator`

4.2.0
-----

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;

/** @Entity */
class SingleIntIdWithBooleanEntity
{
/** @Id @Column(type="integer") */
protected $id;

/** @Column(type="boolean", nullable=true) */
public $enabled;

public function __construct($id, $enabled)
{
$this->id = $id;
$this->enabled = $enabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdStringWrapperNameEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdWithBooleanEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\Type\StringWrapper;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
Expand Down Expand Up @@ -146,6 +147,7 @@ private function createSchema(ObjectManager $em)
$schemaTool = new SchemaTool($em);
$schemaTool->createSchema([
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'),
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdWithBooleanEntity'),
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity'),
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity'),
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNullableNameEntity'),
Expand Down Expand Up @@ -807,4 +809,52 @@ public function testValidateUniquenessCause()
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}

public function testValidateUniquenessWithBooleanFalseValueWithIgnoreFalseEnabled()
{
$constraint = new UniqueEntity([
'message' => 'myMessage',
'fields' => ['enabled'],
'em' => self::EM_NAME,
'ignoreFalse' => true,
]);

$entity1 = new SingleIntIdWithBooleanEntity(1, false);
$entity2 = new SingleIntIdWithBooleanEntity(2, false);

$this->em->persist($entity1);
$this->em->persist($entity2);
$this->em->flush();

$this->validator->validate($entity1, $constraint);

$this->assertNoViolation();
}

public function testValidateUniquenessWithBooleanFalseValueWithIgnoreFalseDisabled()
{
$constraint = new UniqueEntity([
'message' => 'myMessage',
'fields' => ['enabled'],
'em' => self::EM_NAME,
'ignoreFalse' => false,
]);

$entity1 = new SingleIntIdWithBooleanEntity(1, false);
$entity2 = new SingleIntIdWithBooleanEntity(2, false);

$this->em->persist($entity1);
$this->em->persist($entity2);
$this->em->flush();

$this->validator->validate($entity1, $constraint);

$this->buildViolation('myMessage')
->atPath('property.path.enabled')
->setParameter('{{ value }}', 'false')
->setInvalidValue(false)
->setCause([$entity1, $entity2])
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class UniqueEntity extends Constraint
public $fields = [];
public $errorPath = null;
public $ignoreNull = true;
public $ignoreFalse = false;

protected static $errorNames = [
self::NOT_UNIQUE_ERROR => 'NOT_UNIQUE_ERROR',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function validate($entity, Constraint $constraint)
/* @var $class \Doctrine\Common\Persistence\Mapping\ClassMetadata */

$criteria = [];
$hasNullValue = false;
$needBreakValidation = false;

foreach ($fields as $fieldName) {
if (!$class->hasField($fieldName) && !$class->hasAssociation($fieldName)) {
Expand All @@ -91,11 +91,8 @@ public function validate($entity, Constraint $constraint)

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

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

if ($constraint->ignoreNull && null === $fieldValue) {
if ($this->needBreakValidation($constraint, $fieldValue, $class->getTypeOfField($fieldName))) {
$needBreakValidation = true;
continue;
}

Expand All @@ -110,8 +107,8 @@ 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) {
// validation doesn't fail if one of the fields is null (or false) and if null (or false) values should be ignored
if ($needBreakValidation) {
return;
}

Expand Down Expand Up @@ -219,4 +216,21 @@ private function formatWithIdentifiers(ObjectManager $em, ClassMetadata $class,

return sprintf('object("%s") identified by (%s)', $idClass, implode(', ', $identifiers));
}

/**
* Validation doesn't fail and must be stopped if one of the fields is
* `null`|`false` and `null`|`false` values should be ignored.
*/
private function needBreakValidation(UniqueEntity $constraint, $fieldValue, ?string $fieldType): bool
{
if ($constraint->ignoreNull && null === $fieldValue) {
return true;
}

if ($constraint->ignoreFalse && false === $fieldValue && 'boolean' === $fieldType) {
return true;
}

return false;
}
}