【问题标题】:Set the email manually with FOSUserBundle使用 FOSUserBundle 手动设置电子邮件
【发布时间】:2013-08-05 20:16:13
【问题描述】:

我正在使用 Symfony 2 + FOSUserBundle 来管理我的用户。 问题是我想从用户名“手动”构建电子邮件地址,因此不需要任何电子邮件字段。

我将使用用户名创建电子邮件,因为注册的一个要求是拥有来自我的大学的遵循严格格式 (username@schoolname.fr) 的电子邮件。

我设法覆盖了 RegistrationFormType 以避免将电子邮件字段添加到我的注册页面,但我在提交表单时仍然出现“请输入电子邮件”错误。

如何防止验证电子邮件地址以及如何从用户名“构建”它?

谢谢!

(对不起英语,我知道它并不完美......)

【问题讨论】:

    标签: symfony fosuserbundle


    【解决方案1】:

    有一个简单的方法。甚至是mentioned in the official FOSUserBundle documentation。你只需要覆盖控制器。

    创建您自己的 Bundle 并扩展 FOSUserBundle:

    class CustomUserBundle extends Bundle
    {
        public function getParent()
        {
            return 'FOSUserBundle';
        }
    }
    

    然后覆盖RegistrationController:

    class RegistrationController extends BaseController
    {
        public function registerAction(Request $request)
        {
            // here you can implement your own logic. something like this:
            $user = new User();
            $form = $this->container->get('form.factory')->create(new RegistrationType(), $user);
            if ($request->getMethod() == 'POST') {
                $form->bind($request);
                if ($form->isValid()) {
                    $user->setEmail($user->getUsername() . '@' . $user->getSchoolname() . '.fr');
                    // and what not. Also don't forget to either activate the user or send an activation email
    
                }
            }
        }
    }
    

    【讨论】:

    • 嗯,这是一个很好的解决方案,但是仅仅管理一个字段是不是有点太过分了?
    • 还有其他方法。但这是恕我直言,最干净的方式。如果您以后想要修改此内容或添加新字段/删除一些内容,则可以轻松扩展此解决方案。
    【解决方案2】:

    您应该为fos_user.registration.initialize 编写事件监听器。来自代码文档:

    /**
     * The REGISTRATION_INITIALIZE event occurs when the registration process is initialized.
     *
     * This event allows you to modify the default values of the user before binding the form.
     * The event listener method receives a FOS\UserBundle\Event\UserEvent instance.
     */
    const REGISTRATION_INITIALIZE = 'fos_user.registration.initialize';
    

    有关事件调度​​程序的更多信息:http://symfony.com/doc/current/components/event_dispatcher/introduction.html 以及示例事件监听器:http://symfony.com/doc/current/cookbook/service_container/event_listener.html

    更新 - 如何编码?

    在您的config.yml(或services.yml 或其他扩展名,如xmlphp)中定义如下服务:

    demo_bundle.listener.user_registration:
        class:        Acme\DemoBundle\EventListener\Registration
        tags:
            - { name: kernel.event_listener, event: fos_user.registration.initialize, method: overrideUserEmail }
    

    接下来,定义监听类:

    namespace Acme\DemoBundle\EventListener;
    
    class Registration
    {
        protected function overrideUserEmail(UserEvent $args)
        {
            $request = $args->getRequest();
            $formFields = $request->get('fos_user_registration_form');
            // here you can define specific email, ex:
            $email = $formFields['username'] . '@sth.com';
            $formFields['email'] = $email;
            $request->request->set('fos_user_registration_form', $formFields);
        }
    }
    

    注意:当然,您可以通过向侦听器注入 @validator 来验证此电子邮件。

    现在您应该在注册表单中隐藏email 字段。您可以通过覆盖 register_content.html.twig 或(以我认为更好的方式)覆盖 FOS RegistrationFormType 来做到这一点,如下所示:

    namespace Acme\DemoBundle\Form\Type;
    
    use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
    use Symfony\Component\Form\FormBuilderInterface;
    
    class RegistrationFormType extends BaseType
    {
        // some code like __construct(), getName() etc.
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                // some code for your form builder
                ->add('email', 'hidden', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
            ;
        }
    }
    

    现在您的应用程序已准备好手动设置电子邮件。

    【讨论】:

    • 只要他使用相同的表单,验证总是会失败
    猜你喜欢
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2020-03-22
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多