Skip to content

Commit fc94456

Browse files
94nonijaviereguiluz
authored andcommitted
[Form][Validator] Add new unique entity validation on form type
1 parent e6f9e62 commit fc94456

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

reference/constraints/UniqueEntity.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,29 @@ between all of the rows in your user table:
126126
}
127127
}
128128
129+
// src/Form/Type/UserType.php
130+
namespace App\Form\Type;
131+
132+
// ...
133+
// DON'T forget the following use statement!!!
134+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
135+
136+
class UserType extends AbstractType
137+
{
138+
// ...
139+
140+
public function configureOptions(OptionsResolver $resolver): void
141+
{
142+
$resolver->setDefaults([
143+
// ...
144+
'data_class' => User::class,
145+
'constraints' => [
146+
new UniqueEntity(fields: ['email']),
147+
],
148+
]);
149+
}
150+
}
151+
129152
.. caution::
130153

131154
This constraint doesn't provide any protection against `race conditions`_.

0 commit comments

Comments
 (0)