Symfony 2.2通过服务创建表单
在Symfony 2.2中,我想通过服务创建一个小的联系人表单。我使用了服务配置和表单组件工厂,但每次都出现以下异常:Symfony 2.2通过服务创建表单,symfony,symfony-forms,Symfony,Symfony Forms,在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\Compon
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% }
如果有人能给我一些建议,那就太好了,谢谢。你为什么要用双重配额来围绕你的论点 尝试以这种方式更改(您不需要字符串,但我认为您根本不需要字符串)段落中的参数:
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% }
记得
错误报告很重要,您应该仔细阅读它们,因为它们对您有很大帮助由于拼写错误,我也遇到了同样的问题:
和工厂方法
是错误的,工厂服务
和工厂方法
是正确的工厂服务
%parameter%
用作表单的别名
- 如果您仍然有问题,请尝试放置原始字符串(在您的情况下,
anchorbrands\u commonbundle\u contactformtype
)
您的anchorbrands\u common.footer.contact.form
服务中缺少参数,第一个参数似乎错误:
- 请尝试:
[anchorbrands\u commonbundle\u contactformtype,anchorbrands\u commonbundle\u contactformtype,null]
(表单类型的表单名称和别名可以相同)
- 如果有效,请尝试使用
%anchorbrands\u common.contact.form.name%
等效参数
我希望这能帮助你
参考资料:谢谢您的回复!我之所以使用引号,是因为文档中描述了它:服务:新闻通讯\u管理器:类:“%newsletter\u管理器.class%”参数:[“@mailer”,“@templating”]nevermind。。它仍然不起作用,并表示给定了一个字符串:(@user1987373:再说一遍,你做错了什么。为什么你要用双倍配额来包围@mailer
和@templating
?因为你可以在这里看到文档中对其进行了描述!!!symfony.com/doc/master/book/service_container.html,但正如我所说的,我仍然得到了一个例外,没有quotes@user1987373:那太好了因为它需要一个字符串作为输入!错误告诉你:你应该给我一个对象,但你给了我一个字符串。好的,你在哪里传递字符串?我不明白。我传递%anchorbrands\u common.contact.form.type.class%作为参数,它是形式类型anchorbrands\Bundle\CommonBundle\form\type\ContactFormType,工厂为什么要把它当作一根绳子?