繁体   English   中英

尝试将字段添加到注册表单时无法加载类型“ app_user_registration”

[英]Could not load type “app_user_registration” when attempting to add fields into registration form

我正在使用Symctony3中的Doctrine和FOSBundle 2.0开发应用程序。

我一直试图在我的注册栏中添加两个字段: firstNamelastName

不幸的是,在前几个步骤(处理之前)之后, 我发现本教程介绍了如何精确地执行我希望的操作:我意识到我遇到了这个错误:

无法加载类型“ app_user_registration”

我使用的代码完全是从网络上复制的,唯一的区别是我的班级看起来像这样

<?php
// src/AppBundle/Entity/User.php

namespace AppBundle\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="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /** @ORM\Column(type="integer") */
    protected $carma;

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @Assert\NotBlank(message="Please enter your first 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 $firstName;

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @Assert\NotBlank(message="Please enter your first 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 $lastName;

当有一个名为...“ name”的变量时,会产生相同的效果


我花了最后几个小时来弄清楚。 我曾尝试过:

其他所有内容都将我的输出代码显示为该问题的答案

谁能帮助一个可怜的灵魂?

另外,这是我的其他文件:

//config.yml
/*...*/
fos_user:
    db_driver: orm
    firewall_name: main
    user_class: AppBundle\Entity\User
    registration:
        form:
            type: AppBundle\Form\RegistrationType

//services.yml
services:
    app.form.registration:
        class: AppBundle\Form\RegistrationType
        tags:
            - { name: form.type, alias: app_user_registration }

//RegistraionType.php
<?php
// src/AppBundle/Form/RegistrationType.php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

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

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

    public function getName()
    {
        return $this->getBlockPrefix();
    }

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

您应该改用此教程(2.0.master): http ://symfony.com/doc/master/bundles/FOSUserBundle/overriding_forms.html

您正在使用教程“ 1.3.x版本”。

对于Symfony3,您必须进行以下更改:

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

    public function getParent()
    {
        return 'FOS\UserBundle\Form\Type\RegistrationFormType';
    }

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

}
services:
    app.form.registration:
        class: AppBundle\Form\RegistrationType
        tags:
            - { name: form.type, alias: app_user_registration }
fos_user:
    # ...
    registration:
        form:
            type: AppBundle\Form\RegistrationType

我怀疑您一直在尝试遵循扩展注册表的教程。 您很可能应该在app / config / services.yml文件中具有以下条目:

app.form.registration:
    class: AppBundle\Form\RegistrationType
    tags:
        - { name: form.type, alias: app_user_registration }

然后,在您的src / AppBundle / Form / RegistrationType.php中,您应该具有以下功能:

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

最有可能是错误来自您的getBlockPrefix()方法中。 检查您的services.yml并与我发布的内容进行比较。 我从自己的应用程序中获取了该文件,该文件运行良好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM