Search code examples
symfonytwigformbuilder

Printing a Form more than once. Symfony2. Twig Templates


I'm using symfony2. I have a form, and I want to print it a lot of times on a twig template.

This is what I have in the Controller:

$em=$this->getDoctrine()->getEntityManager();

$pruebas = $em->getRepository('UnetPBundle:prueba')->findAll();

$form = $this->createForm(new PruebaType(), $pruebas);

return $this->render('UnetPBundle:Nomina:prueba.html.twig', array(

'form' => $form->createView(),  

'pruebas' => $pruebas                       

));

And this is the content of twig template.

<form action="{{ path('UnetPBundle_prueba') }}" method="post" {{ form_enctype(form) }}  class="sofla">

{{ form_errors(form) }} 

{% for prueba in pruebas %}

{{ form_label(form.nombre, 'Nombre')}}
{{ form_errors(form.nombre)}}
{{ form_widget(form.nombre, {'attr':{'value':prueba.nombre}})}}

{% endfor %}

<input type="submit" value="Submit" />

{{ form_rest(form) }}
</form>

It's printing the field just once.


Solution

  • I think you'll need to create a form from a CollectionType() initialized with $puebras. There will be no need for a loop in your template.