2

I have a custom registration form type defined like this:

....
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $builder
            ->remove('username')
            ->add('firstName')
            ->add('lastName')
            ->add('hei', 'entity', array(
                'class' => 'AcmeAcmeBundle:HigherEducationalInstitution',
                'label' => 'Higher Educational Institution'
            ));

    }
....

The custom controller works pretty much the same as the one in the FOSUserbundle and also checks for a valid form

...
public function registerAsStudentAction(Request $request)
    {
        /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
        $formFactory = $this->get('acme.user_form_factory');
        /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
        $userManager = $this->get('fos_user.user_manager');
        /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
        $dispatcher = $this->get('event_dispatcher');

        $user = $userManager->createUser();
        $user->setEnabled(true);

        $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, new UserEvent($user, $request));

        $form = $formFactory->getStudentRegistrationForm();
        $form->setData($user);

        if ('POST' === $request->getMethod()) {
            $form->bind($request);

            if ($form->isValid()) {
                $event = new FormEvent($form, $request);
                $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);

                $user->addRole('ROLE_STUDENT');

                $userManager->updateUser($user);


                if (null === $response = $event->getResponse()) {
                    $url = $this->get('router')->generate('fos_user_registration_confirmed');
                    $response = new RedirectResponse($url);
                }

                $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

                return $response;
            }
        }

        return $this->render('AcmeUserBundle:Registration:register_student.html.twig', array('form' => $form->createView()));
    }
....

When I try to register with an email address that's already in use I'm getting a doctrine exception for a duplicate entry for a unique key on the email.

In the normal registration form I'm getting a form error displaying that the email address was already used. How can the form pass the validator with a duplicate email address in my form but not in the original registration form?

Simon
  • 1,643
  • 7
  • 30
  • 61
  • Please post also your user entity – Emii Khaos Jun 21 '13 at 08:37
  • @Pazi I'm extending the FOSUserBundle/Model/User class as defined in the documentation. – Simon Jun 21 '13 at 08:51
  • 1
    Symfony's form validator only checks that an email meets the criteria of an email. The form validator is not checking whether your form data is valid against your database schema, in this case unique email entries. – JonnyS Jun 21 '13 at 14:21

2 Answers2

2

Fixed it by adding extra validation.yml to AcmeBundle/Resources/config

Acme\UserBundle\Entity\User:
    constraints:
        - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: { fields: email, message: "This email has already been registered"}
        - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: emailCanonical
    properties:
        email:
            - Email: ~
        emailCanonical:
            - Email:  ~
        plainPassword:
            - Length:
                min: 7
                minMessage: "Your password must be at least {{ limit }} characters"
Simon
  • 1,643
  • 7
  • 30
  • 61
1

You can add this annotation on entity:

@UniqueEntity(fields="email", message="Email already taken")
AythaNzt
  • 1,057
  • 6
  • 14