Skip to content

Commit cb7247a

Browse files
committed
minor #18468 [Form][Validator] Add new unique entity validation on form type (94noni)
This PR was submitted for the 6.3 branch but it was squashed and merged into the 5.4 branch instead. Discussion ---------- [Form][Validator] Add new unique entity validation on form type Hello, Where we have entity without validation on it, and want validation on the form type, this can be valuable I think >**Note**I did not found such info on the doc so after slack discuss, I got the answer and thus proposed this doc Commits ------- fc94456 [Form][Validator] Add new unique entity validation on form type
2 parents e6f9e62 + fc94456 commit cb7247a

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)