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

2023-08-18php开发问题
2

本文介绍了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 扩展注册表导致重复的电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17