繁体   English   中英

可捕获的致命错误:传递给UserBundle \\ Form \\ UserType :: __ construct()的参数2必须是实例?

[英]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.

相关问题 可捕获的致命错误:传递给AppBundle \\ Form \\ TagType :: __ construct()的参数1必须是Doctrine \\ ORM \\ EntityRepository的实例,未给出任何实例, 可捕获的致命错误:传递给“…\\ FormType :: __ construct()的参数1必须实现接口 可捕获的致命错误:传递给 Album\Controller\AlbumController::__construct() 的参数 1 必须是 Album\Model\AlbumTable 的实例,没有给出 可捕获的致命错误:传递给Controller :: __ construct()的参数1必须是Doctrine \\ ORM \\ EntityManager的实例,未给出任何实例,称为 Symfony 2可捕获的致命错误:传递给Sg \\ DatatablesBundle \\ Datatable \\ :: __ construct()的参数1必须是的实例 可捕获的致命错误:传递给...的参数1必须是...,给定数组的实例 切换表时出现zend db错误可捕获的致命错误:传递给__construct()的参数1必须是一个数组,给定对象,在 可捕获的致命错误:传递给UsernamePasswordToken :: __ construct()的参数4必须是一个数组,给定null Symfony2:ContextErrorException:可捕获的致命错误:传递给[…] :: __ construct()的参数1必须实现接口[…]没有给出 可捕获的致命错误:传递给 Illuminate\\Config\\Repository::__construct() 的参数 1 必须是数组类型,给定整数
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM