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"
问题描述
$ 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 ofSymfony\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屋!