Skip to content
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add suggestions
  • Loading branch information
ker0x committed Jul 31, 2025
commit a7cfb4fca59d7780df477ceabb4bf02f7531802d
36 changes: 29 additions & 7 deletions reference/constraints/UniqueEntity.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
// src/Entity/User.php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

Check failure on line 84 in reference/constraints/UniqueEntity.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Doctrine\ORM\Mapping" does not exist
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;

Expand All @@ -100,6 +100,27 @@
}
}

// src/Form/Type/UserType.php
namespace App\Form\Type;

use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

class UserType extends AbstractType
{
// ...

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
// ...
'data_class' => User::class,
'constraints' => [
new UniqueEntity(fields: ['email']),
],
]);
}
}

.. warning::

This constraint doesn't provide any protection against `race conditions`_.
Expand All @@ -116,13 +137,14 @@
Using a PHP class
-----------------

This constraint can also check **uniqueness on any PHP class** and not only on
Doctrine entities. Consider the following Doctrine entity::
This constraint can also check **uniqueness on any PHP class** that is mapped to
a Doctrine entity, and not only on Doctrine entities. Consider the following
Doctrine entity::

// src/Entity/User.php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

Check failure on line 147 in reference/constraints/UniqueEntity.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Doctrine\ORM\Mapping" does not exist

#[ORM\Entity]
class User
Expand Down Expand Up @@ -160,8 +182,6 @@
)]
class UserDto
{
public ?int $id = null,

#[Assert\Email]
public ?string $email = null;
}
Expand Down Expand Up @@ -208,8 +228,6 @@

class UserDto
{
public ?int $id = null;

public ?string $email = null;

public static function loadValidatorMetadata(ClassMetadata $metadata): void
Expand Down Expand Up @@ -353,7 +371,7 @@
as long as they don't have the same name also).

If you need to require two fields to be individually unique (e.g. a unique
``email`` and a unique ``username``), you should use two ``UniqueEntity`` entries,
``email`` and a unique ``username``), you must use two ``UniqueEntity`` entries,
each with a single field.

When `using a PHP class`_, the names of the unique fields may differ
Expand Down Expand Up @@ -721,6 +739,10 @@
}
}

.. note::

This option has no effect when the constraint is applied to an entity.

``message``
~~~~~~~~~~~

Expand Down
Loading