如何添加一个登记表另一个领域的Symfony 2.7 FOSUserBundle(How to ad

2019-10-24 08:10发布

如何登记表上增加一个字段? 我这样做:

\\src\UserBundle\Form\Type\RegistrationFormType.php
    <?php 
    namespace UserBundle\Form\Type;

    //use Symfony\Component\Form\AbstractType;
    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);
            $builder->add('realname');
        }

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

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

\\src\UserBundle\Resources\config 
services:
     app_user.registration.form.type:
        class: UserBundle\Form\Type\RegistrationFormType
        arguments: [%fos_user.model.user.class%]
        tags:
        - { name: form.type, alias: app_user_registration }

\\src\UserBundle\Entity\User.php
namespace 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="web_user")
 */
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="Please enter your name.", groups={"Registration", "Profile"})
     * @Assert\Length(
     *     min=3,
     *     max=255,
     *     minMessage="The name is too short.",
     *     maxMessage="The name is too long.",
     *     groups={"Registration", "Profile"}
     * )
     */
    protected $realname;



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

    /**
     * Set realname
     *
     * @param string $realname
     * @return User
     */
    public function setRealname($realname)
    {
        $this->realname = $realname;

        return $this;
    }

    /**
     * Get realname
     *
     * @return string 
     */
    public function getRealname()
    {
        return $this->realname;
    }
}

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

我不明白什么是缺少的结果是一个错误:未能加载类型“app_user_registration”请帮助我! 我认为,服务不找到

Answer 1:

我有同样的问题。 (其实我发现您的文章由谷歌搜索的错误,我们都有过),我刚开始学习,并用symfony玩弄,所以请借此与一粒盐。

在config.yml试试这个:

  1. 删除你写的服务,
  2. :其替换为fos_user: db_driver: orm firewall_name: main user_class: UserBundle\Entity\User registration: form: type: app_user_registration


文章来源: How to add a registration form another field Symfony 2.7 in FOSUserBundle