![](/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.