![](/img/trans.png)
[英]Catchable Fatal Error: Argument 1 passed to AppBundle\Form\TagType::__construct() must be an instance of Doctrine\ORM\EntityRepository, none given,
[英]Catchable Fatal Error: Argument 2 passed to UserBundle\Form\UserType::__construct() must be an instance ?
我正在尝试在“自定义表单字段类型”中获取当前用户。
我的formType
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class UserType extends AbstractType {
protected $doctrine;
protected $tokenStorage;
public function __construct($doctrine,TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
$this->doctrine = $doctrine;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$user = $this->tokenStorage->getToken()->getUser();
$builder
->setAction($options['data']['url'])
->setMethod('GET')
->add('userType', 'choice', array('choices' => array(
'userType_p' => $pId,
'userType_t' => $tId),
'choices_as_values' => true, 'label' => 'Usertype ',
'expanded' => true, 'multiple' => true,
'translation_domain' => 'User',))........
....
这是我的服务:
user.form.token:
class: UserBundle\Form\UserType
arguments: ['@security.token_storage']
tags:
- { name: form.type }
在控制器中,我这样调用表单:
$form = $this->createForm(new UserType($em,$this->get('user.form.token')), $data....
我收到以下错误消息:
可捕获的致命错误:传递给UserBundle \\ Form \\ UserType :: __ construct()的参数2必须实现接口Symfony \\ Component \\ Security \\ Core \\ Authentication \\ Token \\ Storage \\ TokenStorageInterface,未给出,在其中调用……
UserType::__construct
方法签名在此处具有两个参数,并且您仅在服务声明( $doctrine
)中传递一个参数,因此会出现错误。 如果您仍然需要表单类型的Doctrine,则还应该传递它:
user.form.token:
class: UserBundle\Form\UserType
arguments: ['@doctrine', '@security.token_storage']
tags:
- { name: form.type }
同样,看起来您没有正确创建表单本身,而不是实例化类型本身,您应该仅传递其类名,如Christophe Coevoet所述 :
$form = $this->createForm(UserType::class, $data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.