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.