我需要将我的表单定义为服务,以便我可以向其中注入一些依赖项(例如Entity Manager)。我按照http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html#creating-your-field-type-as-a-service上的教程,但结果是
捕获致命错误:参数1传递给MyFirm \ Form \ ItemAmountType :: __ construct()必须是MyFirm \ Form \ EntityManager的实例,没有给出,在/ var / www / html / ordering-system / vendor /中调用第85行的symfony / symfony / src / Symfony / Component / Form / FormRegistry.php已定义
我有一个注册为服务的表单:
services:
my_firm.form.item_amount:
class: MyFirm\Form\ItemAmountType
arguments: [ "@doctrine.orm.entity_manager", "@price_list_manager" ]
然后我在控制器中创建表单:
$itemAmount = new ItemAmount();
$form = $this->createForm( ItemAmountType::class, $itemAmount );
表格的构造:
class ItemAmountType extends AbstractType {
private $em;
private $priceListManage;
public function __construct(EntityManager $entityManager, PriceListManager $priceListManager) {
$this->em = $entityManager;
$this->priceListManage = $priceListManager;
}
//...
}
问题出在哪里?
答案 0 :(得分:1)
我遗漏了
中的表单标记service:
tags: [{ name: form.type }]