Symfony2 FOSUserBundle扩展注册表导致重复的电子邮件进行验证 [英] Symfony2 FOSUserBundle extending registration form causes duplicate email to validate

查看:65
本文介绍了Symfony2 FOSUserBundle扩展注册表导致重复的电子邮件进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样定义的自定义注册表单类型:

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

    }
....

自定义控制器有效与FOSUserbundle中的代码几乎相同,并且还会检查有效表单

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?

推荐答案

通过在AcmeBundle / Resources / config中添加额外的validate.yml来解决该问题。

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"

这篇关于Symfony2 FOSUserBundle扩展注册表导致重复的电子邮件进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆