понедельник, 19 января 2015 г.

Symfony2 Forms and Doctrine Entity: Unique field values validation

Symfony2 Forms and Doctrine Entity: Unique field values validation

If you’re using Symfony2 forms paired with Doctrine entities, you might sometimes need to validate fields so that the values in that field is always unique. Say – “username” field or “email” field. Two users must not have the same username or email.
There is a form validation constraint – “Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity” which integrates nicely with your entity and allows unique values validation when the form is submitted. Let’s look at some code samples to see how it works:
Here, the email and username fields will be checked for duplicate values and the defined message will be displayed if duplicate values are found.