createFormBuilder() ->add('field_name') ->getFo-6ren">
gpt4 book ai didi

php - Symfony2 : form_widget call in twig throws exception "Catchable fatal error ... must be an instance of Symfony\Component\Form\FormView"

转载 作者:IT王子 更新时间:2023-10-28 23:55:38 26 4
gpt4 key购买 nike

当我在 Controller 中创建一个表单时,如下所示:

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

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

...我尝试在这样的 Twig 模板中呈现此表单:

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

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

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 ...

我该如何解决这个问题?

最佳答案

您必须将 Symfony\Component\Form\FormView 的实例而不是 Symfony\Component\Form\Form 的实例传递给您的 View 。

使用...修复此问题

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

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

关于php - Symfony2 : form_widget call in twig throws exception "Catchable fatal error ... must be an instance of Symfony\Component\Form\FormView",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17561313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com