无法覆盖Sonata用户捆绑包注册表单

时间:2015-11-21 15:45:18

标签: forms symfony sonata-admin symfony-sonata sonata-user-bundle

我在尝试覆盖奏鸣曲注册模板时一直收到错误。

我使用EasyExtendsBundle扩展了Sonata用户包,所以我现在有src / Application / Sonata / UserBundle。

编辑: Symfony 2.7,Sonata Admin 2.3,Sonata User dev-master

我在用户实体中添加了一个字段 的 UserEntity.php

<?php

/**
 * This file is part of the <name> project.
 *
 * (c) <yourname> <youremail>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Application\Sonata\UserBundle\Entity;

use Sonata\UserBundle\Entity\BaseUser as BaseUser;

/**
 * This file has been generated by the Sonata EasyExtends bundle ( http://sonata-project.org/bundles/easy-extends )
 *
 * References :
 *   working with object : http://www.doctrine-project.org/projects/orm/2.0/docs/reference/working-with-objects/en
 *
 * @author <yourname> <youremail>
 */
class User extends BaseUser
{
    /**
     * @var integer $id
     */
    protected $id;

   /*
     * @ORM\Column(type="string", length=255)
     *
     * @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
     */
     protected $age;

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

     public function getAge()
    {
        return $this->age;
    }

        public function setAge($age)
    {
         $this->age = $age;
    }
}

然后我创建了一个新的RegisterForm 的应用程序/奏/ UserBundle /窗体/类型/ RegisterType.php

<?php

namespace Application\Sonata\UserBundle\Form\Type;

use FOS\UserBundle\Form\Type\RegistrationFormType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class RegisterType extends RegistrationFormType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        parent::buildForm($builder, $options);

        $builder
            ->add('age');
    }

    public function setDefaultOption(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'validation_groups' => array('Default', 'Register')
        ));
    }

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

我告诉sonata_user使用我的表格 的 config.yml

sonata_user:
    profile:
        register:
            form:
                type:               front_user_registration
                handler:            sonata.user.profile.form.handler.default
                name:               front_user_registration_form

fos_user:
    db_driver:      orm # can be orm or odm
    firewall_name:  main

    # if you change the class configuration, please also alter the sonata_user.yml file
    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: front_user_registration
    profile:
        form:
            type:               fos_user_profile
            handler:            fos_user.profile.form.handler.default
            name:               fos_user_profile_form
            validation_groups:  [Authentication]

我宣布为服务 的 services.yml

user.form.register.type:
        class: Application\Sonata\UserBundle\Form\Type\RegisterType
        parent: fos_user.registration.form.type
        tags:
            - { name: form.type, alias: front_user_registration }

当我尝试显示表单时,出现以下错误:

  

捕获致命错误:参数1传递给   Sonata \ UserBundle \ Form \ Handler \ ProfileFormHandler :: process()必须   实现接口FOS \ UserBundle \ Model \ UserInterface,给出布尔值,   呼唤   /Users/sylv/Sites/generajobs/vendor/sonata-project/user-bundle/Controller/RegistrationFOSUser1Controller.php   在第49行并定义

如果我添加

也是如此
arguments: [%fos_user.model.user.class%]

进入我的services.yml配置。

我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:1)

这是一个非常愚蠢的错误,我不得不将config.yml中的“handler”行更改为

handler:            sonata.user.registration.form.handler.default

代替sonata.user.profile.form.handler.default,正如我在S.O上的几个例子中看到的那样。

两个处理程序process()函数除了相同的参数外没有。