2016-12-22 33 views
2

我正在使用symfony2。其实我已经使用表单类型创建表单。但是当我渲染的形式,出现以下错误如何解决ContextErrorException:可捕获的致命错误:

ContextErrorException: Catchable Fatal Error: Argument 1 passed to Symfony\Component\Form\FormRenderer::searchAndRenderBlock() must be an instance of 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> 

什么我在做什么错误?

+3

'$ myForm-> createView();' – Beginner

回答

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 
)); 
+0

它的工作!谢谢..我很愚蠢;) – Janvi

相关问题