我在Symfony 2.2中我想通过服务创建一个小联系表单。我使用了服务配置和表单组件工厂,但每次我都遇到以下异常:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Argument 1 passed to Symfony\Component\Form\Form::__construct() must be an instance of Symfony\Component\Form\FormConfigInterface, string given
parameters:
anchorbrands_common.contact.form.type.class: Anchorbrands\Bundle\CommonBundle\Form\Type\ContactFormType
anchorbrands_common.contact.form.name: anchorbrands_commonbundle_contactformtype
services:
anchorbrands_common.footer.contact.form:
class: Symfony\Component\Form\Form
factory-method: createNamed
factory-service: form.factory
arguments: ["%anchorbrands_common.contact.form.type.class%", "null"]
anchorbrands_common.footer.contact.form.type:
class: %anchorbrands_common.contact.form.type.class%
arguments: ["null"]
tags:
- { name: form.type, alias: %anchorbrands_common.contact.form.name% }
如果有人可以给我一些建议会很好,谢谢。
答案 0 :(得分:2)
由于拼写错误,我遇到了同样的问题:
factory-method
和factory-service
错误,factory_method
和factory_service
是正确的。我不确定您是否可以使用%parameter%
alias
作为form.type
代码:
anchorbrands_commonbundle_contactformtype
在您的案例中)。 anchorbrands_common.footer.contact.form
服务中缺少一个参数,第一个似乎是错误的:
请尝试:[anchorbrands_commonbundle_contactformtype, anchorbrands_commonbundle_contactformtype, null]
(表单名称和表单类型的别名可以相同)
如果有效,请尝试使用%anchorbrands_common.contact.form.name%
等效参数。
我希望这可以帮到你。
答案 1 :(得分:0)
为什么用双重配额包围你的论点?
尝试改变(你不需要字符串的地方,但我想你根本不需要字符串)这样的参数传递:
services:
anchorbrands_common.footer.contact.form:
class: Symfony\Component\Form\Form
factory-method: createNamed
factory-service: form.factory
arguments: [%anchorbrands_common.contact.form.type.class%, null]
anchorbrands_common.footer.contact.form.type:
class: %anchorbrands_common.contact.form.type.class%
arguments: [null]
tags:
- { name: form.type, alias: %anchorbrands_common.contact.form.name% }
错误报告非常重要,您应该仔细阅读它们,因为它们可以为您提供很多帮助