Skip to content

Commit ee9b2c4

Browse files
authored
Add new unique entity validation on form type
1 parent 0b4e088 commit ee9b2c4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

reference/constraints/UniqueEntity.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,30 @@ between all of the rows in your user table:
103103
}
104104
}
105105
106+
// src/Form/Type/UserType.php
107+
namespace App\Form\Type;
108+
109+
// ...
110+
// DON'T forget the following use statement!!!
111+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
112+
113+
use Symfony\Component\Validator\Constraints as Assert;
114+
115+
class UserType extends AbstractType
116+
{
117+
// ...
118+
119+
public function configureOptions(OptionsResolver $resolver): void
120+
{
121+
$resolver->setDefaults([
122+
'data_class' => User::class,
123+
'constraints' => [
124+
new UniqueEntity(fields: ['email']),
125+
]
126+
]);
127+
}
128+
}
129+
106130
.. caution::
107131

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

0 commit comments

Comments
 (0)