Skip to content

Commit a7cfb4f

Browse files
committed
Add suggestions
1 parent 11852a6 commit a7cfb4f

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

reference/constraints/UniqueEntity.rst

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,27 @@ between all of the rows in your user table:
100100
}
101101
}
102102
103+
// src/Form/Type/UserType.php
104+
namespace App\Form\Type;
105+
106+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
107+
108+
class UserType extends AbstractType
109+
{
110+
// ...
111+
112+
public function configureOptions(OptionsResolver $resolver): void
113+
{
114+
$resolver->setDefaults([
115+
// ...
116+
'data_class' => User::class,
117+
'constraints' => [
118+
new UniqueEntity(fields: ['email']),
119+
],
120+
]);
121+
}
122+
}
123+
103124
.. warning::
104125

105126
This constraint doesn't provide any protection against `race conditions`_.
@@ -116,8 +137,9 @@ between all of the rows in your user table:
116137
Using a PHP class
117138
-----------------
118139

119-
This constraint can also check **uniqueness on any PHP class** and not only on
120-
Doctrine entities. Consider the following Doctrine entity::
140+
This constraint can also check **uniqueness on any PHP class** that is mapped to
141+
a Doctrine entity, and not only on Doctrine entities. Consider the following
142+
Doctrine entity::
121143

122144
// src/Entity/User.php
123145
namespace App\Entity;
@@ -160,8 +182,6 @@ following using the `entityClass`_ option:
160182
)]
161183
class UserDto
162184
{
163-
public ?int $id = null,
164-
165185
#[Assert\Email]
166186
public ?string $email = null;
167187
}
@@ -208,8 +228,6 @@ following using the `entityClass`_ option:
208228
209229
class UserDto
210230
{
211-
public ?int $id = null;
212-
213231
public ?string $email = null;
214232
215233
public static function loadValidatorMetadata(ClassMetadata $metadata): void
@@ -353,7 +371,7 @@ the combination value is unique (e.g. two users could have the same email,
353371
as long as they don't have the same name also).
354372

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

359377
When `using a PHP class`_, the names of the unique fields may differ
@@ -721,6 +739,10 @@ a key-value mapping:
721739
}
722740
}
723741
742+
.. note::
743+
744+
This option has no effect when the constraint is applied to an entity.
745+
724746
``message``
725747
~~~~~~~~~~~
726748

0 commit comments

Comments
 (0)