如何解决ContextErrorException:Catchable Fatal Error:

时间:2016-12-22 08:29:50

标签: php forms symfony twig

我正在使用symfony2。实际上我已经使用表单类型创建了表单。但是当我渲染表格时,发生以下错误

  

ContextErrorException:Catchable Fatal Error:传递给Symfony \ Component \ Form \ FormRenderer :: searchAndRenderBlock()的参数1必须是Symfony \ Component \ Form \ FormView

的实例

这是我的代码

控制器

$profile = new profile();
$myForm = $this->createForm(new ProfileFormType(), $profile);
return $this->render('ProfileBundle:Profle:profile.html.twig', array(
    'form' => $myForm,
    'vendor' => $vendor,
    'productId' => $productId
));

profile.twig.html

<form action="{{ path('profile_new') }}" method="POST" {{ form_enctype(form) }} id="frmProfile">

    <div class="box-body col-sm-6">
        <div class="form-group">
            <label class="control-label">First Name: </label>
            {{ form_widget(form.firstName, { 'attr': { 'placeholder': 'Title', 'class': 'form-control'}}) }}
            <div class="serverError">{{ form_errors(form.firstName) }}</div>
        </div>
    </div>

    <div class="box-body col-sm-6">
        <div class="form-group">
            <label class="control-label">Last Name: </label>
            {{ form_widget(form.lastName, { 'attr': { 'placeholder': 'Title', 'class': 'form-control'}}) }}
            <div class="serverError">{{ form_errors(form.lastName) }}</div>
        </div>
    </div>

    <div class="box-body col-sm-6">
        <div class="form-group">
            <label class="control-label">User Name: </label>
            {{ form_widget(form.username, { 'attr': { 'placeholder': 'Title', 'class': 'form-control'}}) }}
            <div class="serverError">{{ form_errors(form.username) }}</div>
        </div>
    </div>
</form>

我做错了什么?

1 个答案:

答案 0 :(得分:4)

'form' => $myForm替换为'form' => $myForm->createView()。函数createView()将创建表单对象的视图。

return $this->render('ProfileBundle:Profle:profile.html.twig', array(
    'form' => $myForm->createView(),  // << Add "->createView()"
    'vendor' => $vendor,
    'productId' => $productId
));