覆盖默认FOSUserBundle表单处理程序

时间:2016-03-21 17:36:09

标签: symfony fosuserbundle

我重写了FOSUserBundle表格:

class RegistrationType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->remove('username')
        ;
    }

    public function getParent()
    {
        return 'fos_user_registration';
    }

    public function getName()
    {
        return 'app_user_registration';
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Application\Sonata\UserBundle\Entity\User',
        ));
    }
}

目的是从注册表单中删除用户名字段。 它有效,但当我使用表格时,我有这个错误:

Please enter a username

因此我尝试按照以下文档覆盖FormHandler:https://symfony.com/doc/1.3.x/bundles/FOSUserBundle/overriding_forms.html#overriding-form-handlers

但在我删除字段的情况下,我并没有真正做到这一点?

这是我的yaml文件

# service.yml    
services:
    app.form.registration:
        class: Application\Sonata\UserBundle\Form\RegistrationType
            tags:
                - { name: form.type, alias: app_user_registration }

        app.form.handler.registration:
            class: Application\Sonata\UserBundle\Handler\RegistrationFormHandler
            arguments: ["@fos_user.registration.form", "@request", "@fos_user.user_manager", "@fos_user.mailer", "@fos_user.util.token_generator"]
            scope: request
            public: false

    # config.yml
    fos_user:
        db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
        firewall_name: main
        user_class: Application\Sonata\UserBundle\Entity\User
        group:
            group_class:   Application\Sonata\UserBundle\Entity\Group
            group_manager: sonata.user.orm.group_manager
        service:
            user_manager: sonata.user.orm.user_manager
        registration:
            form:
                type: Application\Sonata\UserBundle\Form\RegistrationType
                handler: app.form.handler.registration

1 个答案:

答案 0 :(得分:1)

我已将此代码添加到我的实体用户中以使其正常工作:

public function setEmail($email)
    {
        parent::setEmail($email);
        $this->setUsername($email);
    }