簡體   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