Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 array of forms?

Is it possible to create and render and array of forms I know about collections but they don't really fit in my idea?

What I want is something like this

Controller

$data=$em->findAll();
$Forms=$this->createForm(new SomeType,$data);

return $this->render(someView,array("Forms"=>$Forms->createView()));

Twig

  {% for Form in Forms %}
  {{ form(Form)}}
  {% endfor %}
like image 887
harisk92 Avatar asked Mar 20 '23 01:03

harisk92


1 Answers

Just create your forms in array:

$data = $em->findAll();
for ($i = 0; $i < $n; $i++) {
    $forms[] = $this->container
        ->get('form.factory')
        ->createNamedBuilder('form_'.$i, new SomeType, $data)
        ->getForm()
        ->createView();
}

return $this->render(someView, array("forms" => $forms));

UPDATED

As mentioned by edlouth you can create each form named separately. I updated my code.

like image 99
Michael Sivolobov Avatar answered Apr 01 '23 23:04

Michael Sivolobov