沃梦达 / 编程问答 / php问题 / 正文

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

Symfony2 FOSUserBundle extending registration form causes duplicate email to validate(Symfony2 FOSUserBundle 扩展注册表导致重复的电子邮件验证)

本文介绍了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 FOSUserBundleFormFactoryFactoryInterface */
        $formFactory = $this->get('acme.user_form_factory');
        /** @var $userManager FOSUserBundleModelUserManagerInterface */
        $userManager = $this->get('fos_user.user_manager');
        /** @var $dispatcher SymfonyComponentEventDispatcherEventDispatcherInterface */
        $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 中添加额外的validation.yml 来修复它

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

AcmeUserBundleEntityUser:
    constraints:
        - SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity: { fields: email, message: "This email has already been registered"}
        - SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity: emailCanonical
    properties:
        email:
            - Email: ~
        emailCanonical:
            - Email:  ~
        plainPassword:
            - Length:
                min: 7
                minMessage: "Your password must be at least {{ limit }} characters"

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

本文标题为:Symfony2 FOSUserBundle 扩展注册表导致重复的电子邮件验证

基础教程推荐