doulun1939 2014-12-21 19:37
浏览 57
已采纳

索引视图中的“删除表单”

Question:

How can I render a 'DeleteForm' in inside my 'Index' view by using {{ form(deleteForms) }}

Description:

I have followed the steps described in this SO answer, however I would like to streamline it a little more.

What is described there is to add the following to your indexAction:

//ProductController.php
//...

$deleteForms = array();

foreach ($entities as $entity) {
$deleteForms[$entity->getId()] = $this->createDeleteForm($entity->getId())->createView();
}
return $this->render('AppBundle:Product:index.html.twig', array(
'entities' => $entities,
'deleteForms' => $deleteForms,
));

And in your view add the following: {% for entity in entities %} //... {{ form_widget(deleteForms[entity.id]) }} {{ 'links.admin.form.delete' | trans({}, 'FooBundle') }} ...// {% endfor %}

Which works fine, my niggle is why render the form manually when anywhere (Update/Show etc) else I just do this:

{% for entity in entities %}
//...
{{ form(deleteForms) }}
...//
{% endfor %}

So I tried this instead but it resulted in this error:

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Argument 1 passed to Symfony\Component\Form\FormRenderer::renderBlock() must be an instance of Symfony\Component\Form\FormView, array given, called in C:\MyBundle\app\cache\dev\twig\f6\75\ae2ae7bebde1f4083ba543a1680ecdb4b1c359141e57ac01862c94660f2e.php on line 176 and defined") in AppBundle:Product:index.html.twig at line 49.

I've Googled the error which suggests I need to be calling "->createView()" but this is already being called in the foreach array. I tried doing it in the Return section anyway as so:

return $this->render('AppBundle:Product:index.html.twig', array(
            'entities' => $entities,
            'deleteForms' => $deleteForms->createView(),
));

All this does is cause another error:

Error: Call to a member function createView() on a non-object

So now I'm completely lost. Any pointers in the right direction would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dsogx84602 2014-12-21 20:52
    关注

    You seem to be creating an array of the "deleteForms" with the entity id as the key so when you call them in the template you should use that same format.

    For example..

    {% for entity in entities %}
        //...
        {{ form(deleteForms[entity.id]) }}
        ...//
    {% endfor %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效