【问题标题】:Symfony FOSUserBundle cannot override form, no effectSymfony FOSUserBundle 无法覆盖表单,没有效果
【发布时间】:2017-04-21 11:42:39
【问题描述】:

有了这个article,我试图覆盖RegisterFormType.php,但看不到结果,但Symfony Profiler 显示在Forms fos_user_registration 被app_user_registration 覆盖。

首先,我在 AppKernel.php 中:

new FOS\UserBundle\FOSUserBundle(),
new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'),
new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),

services.yml

app.form.registration:
    class: Application\Sonata\UserBundle\Form\RegistrationFormType
    tags:
        - { name: form.type, alias: app_user_registration }

config.yml:

fos_user:
    db_driver:      orm # can be orm or odm
    firewall_name:  main
    user_class:     Application\Sonata\UserBundle\Entity\User
    use_listener: true
    group:
        group_class:   Application\Sonata\UserBundle\Entity\Group
        group_manager: sonata.user.orm.group_manager                    # If you're using doctrine orm (use sonata.user.mongodb.group_manager for mongodb)

    service:
        user_manager: sonata.user.orm.user_manager                      # If you're using doctrine orm (use sonata.user.mongodb.user_manager for mongodb)
    registration:
        form:
            type: app_user_registration
            #validation_groups: [AppRegistration]
    profile:
        form:
            name: app_user_profile

src/Application/Sonata/UserBundle/Form/RegistrationForm.php:

namespace Application\Sonata\UserBundle\Form;

use ...

class RegistrationFormType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options)    {
        $builder
            ->add('fullName')
        ;
    }

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


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

src/Application/Sonata/UserBundle/Entity/User.php:

namespace Application\Sonata\UserBundle\Entity;

use Sonata\UserBundle\Entity\BaseUser as BaseUser;

class User extends BaseUser
{
    /**
     * @var int $id
     */
    protected $id;

    /**
     * @var string
     */
    protected $fullName;

    /**
     * @return string
     */
    public function getFullName() {
        return $this->fullName;
    }

    /**
     * @param string $fullName
     */
    public function setFullName($fullName) {
        $this->fullName = $fullName;
    }

    /**
     * Get id
     *
     * @return int $id
     */
    public function getId() { return $this->id;}
}

作为其他方式(我认为不正确)试图在我的自定义目录中覆盖 RegistrationController.php,在那里更改 $form = $this->container->get('app.form.registration')(我的表单的别名),但我收到错误 Attempted to call an undefined method named "createView" of class "Application\Sonata\UserBundle\Form\RegistrationFormType。在此之后,我将这个类的父扩展类从 ContentAware 更改为 Controller,并且可以看到表单和新字段,但提交后收到未知错误,即使在 Profiler 中也没有写出它的名称。

Symfony 2.8.18,FOSUserBundle 1.3.7。感谢您的宝贵时间。

【问题讨论】:

  • 已解决,将config.yml表单名改为type(尽管文档中写了名字。通过debug:container发现了

标签: php symfony overriding fosuserbundle


【解决方案1】:

你更新了 config.yml 中 FOSUserBundle 的配置了吗?

fos_user:
     # ...
     registration:
        form:
            name: app_user_registration

【讨论】:

  • 是的,在上面的帖子中添加了这个文件片段现在的外观。抱歉,忘记立即提及此文件。
猜你喜欢
  • 2013-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多