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 %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python