I am writing an application in which there is a table. You need to edit it with ajax. In each row of the table there are forms so that you can send them to the server. With the help of html forms everything works out, but probably it would be better to use exactly the symfony forms. I tried to realize the forms of a symphony, but I did not understand how to transfer variables there. So, how to implement the output of many symphony forms with different parameters? The table is now displayed like this:

<table class="table"> <thead> <tr> <th>Name</th> <th>Rows</th> <th>Change</th> </tr> </thead> <tbody> {% for garden in gardens %} <tr id="editRow{{ garden.id }}"> <form method="POST" name="garden" id="saveRow{{ garden.id }}" action="javascript:void(null);" > <input type="hidden" value="{{ garden.id }}" name="id" /> <td><input type="text" name="name" value="{{ garden.name }}" class="table-inputs" readonly></td> <td><input type="text" name="rows" value="{{ garden.rows }}" class="table-inputs" readonly></td> <td> <div> <button id="{{ garden.id }}" class="btn btn-default btn-icon glyphicon glyphicon-pencil" data-original-title="Edit"></button> <button id="{{ garden.id }}" class="btn btn-default btn-icon glyphicon glyphicon-remove" data-original-title="Delete"></button> </div> </td> </form> </tr> {% endfor %} </tbody> </table> 

enter image description here

    1 answer 1

    You need to use collections of forms, you can read about them in the documentation .

    You can implement something like this:

    • Create a container class for the Garden class collection:

    // src / AppBundle / Entity / Gardens.php

     namespace AppBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; class Gardens { protected $gardens; public function __construct() { $this->gardens = new ArrayCollection(); } public function getGardens() { return $this->gardens; } } 
    • Garden class itself:

    // src / AppBundle / Entity / Garden.php

     namespace AppBundle\Entity; class Garden { private $name; public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } } 
    • we create the form for Garden:

    // src / AppBundle / Form / Type / GardenType.php

     namespace AppBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class GardenType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('name'); } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\Garden', )); } } 
    • we create the form for Gardens:

    // src / AppBundle / Form / Type / GardensType.php

     namespace AppBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Form\Extension\Core\Type\CollectionType; class GardensType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('gardens', CollectionType::class, array( 'entry_type' => GardenType::class )); } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\Gardens', )); } } 
    • controller:

    // src / AppBundle / Controller / GardensController.php

     namespace AppBundle\Controller; use AppBundle\Entity\Gardens; use AppBundle\Entity\Garden; use AppBundle\Form\Type\GardensType; use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class GardensController extends Controller { public function indexAction(Request $request) { $gardens = new Gardens(); $garden1 = new Garden(); $garden1->setName('garden1'); $gardens->getGardens()->add($garden1); $garden2 = new Garden(); $garden2->setName('garden2'); $gardens->getGardens()->add($garden2); $form = $this->createForm(GardensType::class, $gardens); $form->handleRequest($request); return $this->render('AppBundle:Gardens:index.html.twig', array( 'form' => $form->createView(), )); } } 
    • we draw the form:

      {# src / AppBundle / Resources / views / Gardens / index.html.twig #}

     {{form_start (form)}}
        
             {% for garden in form.gardens%}
                 {{form_row (garden.name)}}
             {% endfor%}
         
     {{form_end (form)}}
    
    • Although the link can find the answer to the question, it is better to point out the most important thing here, and give the link as a source. If the page to which the link leads will be changed, the response link may become invalid. - From the queue of checks - cheops
    • @cheops supplemented the answer - Alexey Shmelev