Search code examples
phpformssymfonysymfony-formsformbuilder

Symfony2 How to create one form for many the same entities?


I have the following entity:

// ZaquPL/MyBundle/Entity/Task.php
protected $name;
protected $order;

public function getName()         {...}
public function setName($name)    {...}
public function getOrder()        {...}
public function setOrder($order)  {...}

I would like one form with many entities. It's becouse I want to sort tasks "drag and drop) and change their names in one form, with one submit button and without AJAX.

It should looks like in that schema:

[GRAB AND MOVE <INPUT name="order[1]">] <INPUT name="name[1]">
[GRAB AND MOVE <INPUT name="order[2]">] <INPUT name="name[2]">
[GRAB AND MOVE <INPUT name="order[3]">] <INPUT name="name[3]">
[GRAB AND MOVE <INPUT name="order[4]">] <INPUT name="name[4]">
[GRAB AND MOVE <INPUT name="order[5]">] <INPUT name="name[5]">
[GRAB AND MOVE <INPUT name="order[6]">] <INPUT name="name[6]">

I know how to create view, how to use jquery UI drag and drop components etc, so don't worry about that. I only ask how to create this form using Symfony FormBuilder. Have you any idea?


Solution

  • I think you want create a collection of form : doc. On the first persistence the order is defined by your request header order. But if you edit this collection you can't just submit another order and persist. To change your collection order you can :

    • remove last collection on edit and persist the new collection
    • use an attribute which manage your order
    • serialize your collection(a little creepy i know)

    I hope it help