Symfony2:树枝中的form_widget调用引发异常“可捕获的致命错误...必须是Symfony组件的一个实例”\\ Form \\“FormView” [英] Symfony2: form_widget call in twig throws exception "Catchable fatal error ... must be an instance of Symfony\Component\Form\FormView"

查看:230
本文介绍了Symfony2:树枝中的form_widget调用引发异常“可捕获的致命错误...必须是Symfony组件的一个实例”\\ Form \\“FormView”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ form = $ this-> createFormBuilder()创建一个表单, 
- > add('field_name')
- > getForm();

返回数组(
'form'=> $ form
);

...然后我试着在这样一个树枝模板中渲染这个表单:

 < form action ={{path('...')}}method =post> 
{{form_widget(form.field_name)}}
< / form>

... form_widget 调用产生以下异常/错误:


在呈现模板期间抛出异常(Catchable致命错误:参数1传递给Symfony \\ \\Component\Form\FormRenderer :: searchAndRenderBlock()必须是Symfony \ Component \Form\FormView的一个实例,Symfony \ Component \Form\Form的实例,如何解决这个问题?

解决方案

您必须传递 Symfony \ Component \Form \FormView 的实例,而不是 Symfony \Component\Form\



使用...解决此问题。

  ...  - > getForm() - > createView(); 

FormBuilder :: getForm 构建表单对象... Form :: createView 然后创建一个 FormView 对象。


When I create a form inside my controller action like this:

$form = $this->createFormBuilder()
    ->add('field_name')
    ->getForm();

return array(
    'form' => $form
);

... and I try to render this form in a twig template like this:

    <form action="{{ path('...') }}" method="post">
        {{ form_widget(form.field_name) }}
    </form>

... the form_widget invocation produces the following exception/error:

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Argument 1 passed to Symfony\Component\Form\FormRenderer::searchAndRenderBlock() must be an instance of Symfony\Component\Form\FormView, instance of Symfony\Component\Form\Form given, called in ...

How can I resolve this issue?

解决方案

You have to pass an instance of Symfony\Component\Form\FormView instead of Symfony\Component\Form\Form to your view.

Fix this using ...

... ->getForm()->createView();

FormBuilder::getForm builds the Form object ... Form::createView then creates a FormView object.

这篇关于Symfony2:树枝中的form_widget调用引发异常“可捕获的致命错误...必须是Symfony组件的一个实例”\\ Form \\“FormView”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆