Symfony FOSUserBundle覆盖表单

时间:2014-10-29 13:01:54

标签: forms symfony fosuserbundle

我正在尝试覆盖FOSUserBundle默认注册表单。确切地说,我正在尝试删除用户名。问题可能在services.yml中。但不确定。 services.yml文件:

services:
    user.registration.form.type:
        class: Atotrukis\MainBundle\Form\Type\RegistrationFormType
        tags:
            - { name: form.type, alias: user_registration }

security.yml

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
            logout:       true
            anonymous:    true

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }

RegistrationFormType.php

<?php

namespace Atotrukis\MainBundle\Form\Type;

use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
use Symfony\Component\Validator\Constraints as Assert;

class RegistrationFormType extends BaseType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        parent::buildForm($builder, $options);
        $builder->remove('username');
    }

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

}

?>

1 个答案:

答案 0 :(得分:0)

您的代码有两个问题。

表单的继承不是通过类扩展来完成的,而是使用getParent()方法的定义。您定义服务名称。例如。

class RegistrationFormType extends AbstractType
{
    public function getParent()
    {
        return 'fos_user_registration';
    }
}

您忘记在捆绑配置中定义类

# app/config/config.yml
fos_user:
    registration:
        form:
            type: user.registration.form.type