Unique validator in Symfony2

I have a user registration form. One of the things that this form should do is to ensure the uniqueness of the user's new email address. In other words, the user's new email address should not be an email address already used by someone else.

I have not yet been able to find an acceptable solution to this problem, which is somewhat surprising given how simple and general this need is (and how simple and simple the solution is in Rails!).

I saw this question / answer , but the problem with this solution is that it seems to confirm “uniqueness” not only when creating the record, but every time you try to update the record. Therefore, as soon as the record is saved, you will no longer be able to update it, because it tells you that the email address is already in use. (Perhaps I am implementing my solution incorrectly, but it does not look like what I am.)

Does anyone have a working example (and not just a link to docs , please, what I already read) uniqueness validator on an entity / form?

+5
source share
2 answers

, . , , "" , , , -, . User. groups={"registration"} @UniqueEntity:

/**
 * VNN\PressboxBundle\Entity\User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 * @UniqueEntity(fields="email", message="Sorry, this email address is already in use.", groups={"registration"})
 * @UniqueEntity(fields="username", message="Sorry, this username is already taken.", groups={"registration"})
 */
class User implements AdvancedUserInterface, \Serializable
{
    /** 
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** 
     * @var string $email
     * @Assert\NotBlank()
     * @Assert\Email()
     *
     * @ORM\Column(name="email", type="string", length=255, unique=true)
     */
    private $email;

    /**
     * @var string $username
     * @Assert\NotBlank()
     *
     * @ORM\Column(name="username", type="string", length=255, unique=true)
     */
    private $username;

( ):

<?php

namespace VNN\PressboxBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class UserType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {   
        $builder
            ->add('first_name')
            ->add('last_name')
            ->add('email')
            ->add('username')
            ->add('password', 'password')
            ->add('athletic_director_email')
            ->add('school')
            ->add('active', 'hidden')
            ->add('transition_key', 'hidden')
        ;   
    }   

    public function getName()
    {   
        return 'vnn_pressboxbundle_usertype';
    }   

    // The following function is what important here.
    // This tells this form to use the "registration" validation group.
    public function getDefaultOptions(array $options)
    {   
        return array(
            'validation_groups' => array('registration')
        );  
    }   
}

, . , - - .

+14

Doctrine, Entity ?

/**
 * @ORM\Column(type="string", unique="true")
 */
protected $email;
-1

All Articles