Symfony2 array of forms?
Asked Answered
A

4

4

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 %}
Amling answered 15/7, 2014 at 0:13 Comment(0)
I
7

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.

Incidence answered 15/7, 2014 at 3:46 Comment(3)
Thx bro, but I got another problem now when I submit one form it send an array of forms the reason is probably cause all submit buttons have same value.How can I solve that ?Amling
You need to generate different forms. What do you want to achieve?Incidence
I want to create list of delete forms for every entity so when I click on delete button I want to call some action pass my form ,get data out of it and remove it using doctrine entity manager.Amling
C
0

Create the forms in an array, but give each one a unique name. I've changed it to formbuilder which might not be ideal for you but hopefully something similar will work. I'm also not certain about putting in new SomeType rather than 'form', see http://api.symfony.com/2.4/Symfony/Component/Form/FormFactory.html#method_createNamedBuilder.

$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));
Christy answered 15/7, 2014 at 13:30 Comment(2)
You could just edit my answer. It has minor modifications from mine.Incidence
I agree I could have done. I was not aware I could edit, given my reputation, as I attempted to comment but wasn't permitted to, hence assumed I had to give a separate answer.Christy
J
0

Symfony3:

$datas = $em->findAll();

foreach ($datas as $key=>$data)
{
   $form_name = "form_".$key;
   $form = $this->get('form.factory')->createNamed( 
      $form_name, 
      SomeType::class, 
      $data
   );
   $views[] = $form->createView();
}
return $this->render(someView, ["forms" => $views]);
Juristic answered 15/6, 2017 at 15:15 Comment(0)
B
0

The action :

$forms = [];

foreach ($articles as $article) {
    $forms[$article->getId()] = $this->get('form.factory')->createNamed(
        'article_'.$article->getId(), // unique form name
        ArticleType::class,
        $article
    );
    $forms[$article->getId()]->handleRequest($request);

    if ($forms[$article->getId()]->isValid()) {
        // do what you want with $forms[$article->getId()]->getData()
        // ...
    }
}

And a better way to render :

return $this->render('some_view.html.twig', [
    'forms' => array_map(function ($form) {
        return $form->createView();
    }, $forms),
]);
Braille answered 5/11, 2018 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.