gpt4 book ai didi

symfony - FOSUser Bundle..覆盖表单类型时无法加载类型 “X”错误

转载 作者:行者123 更新时间:2023-12-02 10:07:57 26 4
gpt4 key购买 nike

我以为我非常仔细地遵循了文档,但由于某种原因,我收到了此错误消息..

无法加载类型“sam_user_registration”

//src/Sam/Bundle/UserBundle/Entity/User.php

namespace Sam\Bundle\UserBundle\Entity;

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

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

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

/**
* @ORM\Column(type="string", length=255)
*
* @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* @Assert\MinLength(limit="3", message="The name is too short.", groups={"Registration", "Profile"})
* @Assert\MaxLength(limit="255", message="The name is too long.", groups={"Registration", "Profile"})
*/
protected $name;

//src/Sam/Bundle/UserBundle/Form/Type/RegistrationFormType.php

<?php

namespace Sam\Bundle\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('name');
}

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

** src/Sam/Bundle/UserBundle/Resources/config/services.xml **

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<service id="sam_user.registration.form.type" class="Sam\Bundle\UserBundle\Form\Type\RegistrationFormType">
<tag name="form.type" alias="sam_user_registration" />
<argument>%fos_user.model.user.class%</argument>
</service>

</container>

# app/config/config.yml

fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main
#user_class: Sam\Bundle\UserBundle\Document\User
user_class: Sam\Bundle\UserBundle\Entity\User

group:
group_class: Sam\Bundle\UserBundle\Entity\Group

profile: # Authentication Form
form:
type: fos_user_profile
name: fos_user_profile_form
validation_groups: [Authentication]

registration:
form:
type: sam_user_registration

最佳答案

首先,您的用户类缺少其构造函数。由于您继承了 BaseUser,因此您也必须调用父构造函数。所以...

public function __construct()
{
parent::__construct();

//your code

}

其次,您不需要覆盖表单类型。由于您已将名称字段添加到您的实体中,因此它将通过以下方式自动在您的表单中可用:

{{ form_widget(myForm.name)} }

-> FosUserBundle Doc

但是,您确定在 config.yml 中导入了 services.xml 并且为什么要混合 xml 和 yml?只要坚持其中一个即可。

imports:
- { resource: @SamBundleUserBundle/Resources/config/services.yml }

帕特在评论中指出了我们的 overriding registration FOSUserBundle Symfony2

关于symfony - FOSUser Bundle..覆盖表单类型时无法加载类型 “X”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14545774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com