From fc94456249d1d888c656d235a669f7b4fb3ce4f8 Mon Sep 17 00:00:00 2001 From: Antoine M Date: Thu, 29 Jun 2023 19:50:07 +0200 Subject: [PATCH] [Form][Validator] Add new unique entity validation on form type --- reference/constraints/UniqueEntity.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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`_.