Closed
Description
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.
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