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

Skip to content

[DoctrineBridge] UniqueEntityValidator strictly compares ID fields that are objects (like UUID) #58883

Closed
@zavarock

Description

@zavarock

Symfony version(s) affected

>=7.1

Description

I'm unsure if the approach taken by the UniqueEntityValidator is appropriate for all situations. Typically, we use the entity object to make changes with a FormType, which usually works well. However, in the case of DTOs, this can be a problem.

if ($fieldValues[$identifierFieldName] !== $propertyValue) {

How to reproduce

  • Make an entity DTO with a UniqueEntity constraint and an ID property which must be a UUID (or any other type of object)
  • Validate this entity DTO with parameters of an existent entity and you should see the problem

DTO sample:

use App\Entity\User;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Validator\Constraints as Assert;

#[UniqueEntity(
    fields: ['name'],
    entityClass: User::class,
    identifierFieldNames: ['id'],
)]
class UserDto
{
    public ?Uuid $id = null;

    #[Assert\NotBlank]
    public ?string $name = null;
}

Possible Solution

Maybe this is an edge case, but I wonder if it would be better to check if the field is an object and, if yes, make a loose one (!=) instead of (!==).

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions