Symfony 2.2通过服务创建表单

时间:2013-04-24 07:17:35

标签: 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\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% }

如果有人可以给我一些建议会很好,谢谢。

2 个答案:

答案 0 :(得分:2)

由于拼写错误,我遇到了同样的问题:

  • factory-methodfactory-service错误,factory_methodfactory_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%等效参数。

我希望这可以帮到你。

参考文献:Define a Symfony 2 Form as a Service

答案 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% }

记住

错误报告非常重要,您应该仔细阅读它们,因为它们可以为您提供很多帮助