diff --git a/reference/constraints/UniqueEntity.rst b/reference/constraints/UniqueEntity.rst index 59840bbfe9b..f0bf8c6879a 100644 --- a/reference/constraints/UniqueEntity.rst +++ b/reference/constraints/UniqueEntity.rst @@ -126,6 +126,29 @@ between all of the rows in your user table: } } + // src/Form/Type/UserType.php + namespace App\Form\Type; + + // ... + // DON'T forget the following use statement!!! + 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']), + ], + ]); + } + } + .. caution:: This constraint doesn't provide any protection against `race conditions`_.