FOSUserBundle重写默认表单并获取InvalidArgumentException

时间:2013-10-23 23:59:41

标签: php symfony fosuserbundle

在覆盖FOSUserBundle docs覆盖表单后,我收到错误消息。我得到的错误是:

  

为服务指定的类型名称" scheduler_user.registration.form.type"与实际名称不符。预期" scheduler_user_registration",给予" fos_user_registration"

以下是相关文件:

RegistrationFormType.php

<?php
// src/Scheduler/UserBundle/Form/Type/RegistrationFormType.php

namespace Scheduler\UserBundle\Form\Type;

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

class RegistrationFormType extends BaseType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        // add your custom field
        $builder->add('firstname');
        $builder->add('lastname');
    }

    public function getFirstname()
    {
        return 'scheduler_user_registration';
    }

    public function getLastname()
    {
        return 'scheduler_user_registration';
    }
}

user.php的

<?php
// src/Scheduler/UserBundle/Entity/User.php

namespace Scheduler\UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

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

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

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

config.yml

# app/config/config.yml

# ...

# FOS User Bundle
fos_user:
    db_driver: orm
    firewall_name: main
    user_class: Scheduler\UserBundle\Entity\User
    registration:
      form:
        type: scheduler_user_registration

services.yml

# src/Scheduler/UserBundle/Resources/config/services.yml
services:
    scheduler_user.registration.form.type:
        class: Scheduler\UserBundle\Form\Type\RegistrationFormType
        arguments: [%fos_user.model.user.class%]
        tags:
            - { name: form.type, alias: scheduler_user_registration }

1 个答案:

答案 0 :(得分:0)

表单类型不需要getFirstnamegetLastname方法。

删除其中一个并将另一个重命名为getName

该方法不是表单字段的getter。它只是指定表单的名称。