Description
Hello,
I'm trying to create this generic jquery plugin to handle form collections. It aims to manage adding, deleting and moving field sets up and down.
That's quite easy to do this on simple collections thanks to the prototype
option, but I'm trying to also manage collections or form collections. For addition and deletion of collections, that's easy, but to move them, that's really hard as we need to update name
attributes accordingly.
If I simplify the problem:
<div id="form_collections" data-prototype="...">
<div id="form_collections_0" data-prototype="...">
<input id="form_collections_0_0_value" name="form[collections][0][0][value]" ... />
<input id="form_collections_0_1_value" name="form[collections][0][1][value]" ... />
// add, delete, move up, move down buttons
</div>
<div id="form_collections_1" data-prototype="...">
<input id="form_collections_1_0_value" name="form[collections][1][0][value]" ... />
<input id="form_collections_1_1_value" name="form[collections][1][1][value]" ... />
// add, delete, move up, move down buttons
</div>
</div>
To swap 2 elements in the child collection form_collections_1
, I just need to replace all names beginning by form[collections][1][positionElemA]
by form[collections][1][positionElemB]
(and vice-versa). But in fact, I am missing the form[collections][1]
prefix.
To swap 2 elements in the parent collection form_collections
, I just need to iterate the very same way throught all form fields contained in the moved elements. I replace all names beginning by form[collections][positionElemA]
by form[collections][positionElemB]
(and vice-versa). I still need to find the form[collections]
prefix somewhere.
The second case is more explicit because it contains no fields having a name attribute, so I can't guess in any way from the prototype or anything else what could be the collection's prefix.
This information, stored in full_name
variable in the form theme, is very easy to get. But that's not friendly to ask my users (or even yours if you begin to work on a similar plugin) to overwrite the Symfony's default theme just to add buttons to their forms, so here come the idea:
Do you think it worth a PR to add this prefix option on collection field type, that will set a data-prefix="{{ full_name }}"
attribute in the collection's container?