Symfony2 FOSUserBundle扩展注册表单会导致重复的电子邮件验证

问题描述 投票:2回答:2

我有一个自定义注册表单类型定义如下:

....
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'
            ));

    }
....

自定义控制器的工作原理与FOSUserbundle中的控制器非常相似,并且还会检查有效的表单

...
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()));
    }
....

当我尝试使用已经在使用的电子邮件地址注册时,我收到了电子邮件中唯一键的重复条目的学说例外。

在正常的注册表单中,我收到一个表单错误,显示电子邮件地址已被使用。表单如何通过我的表单中的重复电子邮件地址传递验证器,但不是原始注册表单?

php symfony doctrine-orm fosuserbundle
2个回答
2
投票

通过向AcmeBundle / Resources / config添加额外的validation.yml来修复它

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"

1
投票

您可以在实体上添加此注释:

@UniqueEntity(fields="email", message="Email already taken")
© www.soinside.com 2019 - 2024. All rights reserved.