Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] Add a prefix option to the colletion type #13837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ninsuo opened this issue Mar 3, 2015 · 8 comments
Closed

[Form] Add a prefix option to the colletion type #13837

ninsuo opened this issue Mar 3, 2015 · 8 comments

Comments

@ninsuo
Copy link

ninsuo commented Mar 3, 2015

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.

sample

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?

@Tobion
Copy link
Contributor

Tobion commented Mar 4, 2015

Why not simply tell the users to set the attr option with the data-prefix? No need for a theme.
Also how would symfony itself use the data-prefix? I guess not at all. Why would it such an option that does nothing and is implementation specific?

@ninsuo
Copy link
Author

ninsuo commented Mar 4, 2015

Hello @Tobion ,

If you have a collection of form collections and simply use {{ form(myForm) }} without iterating through the form (recursively if your form is dynamic enough), that's not possible to set the data-prefix attribute to each collection fields.

I do not agree that this is implementation specific because without the collection's field name, we (the whole community) are limited to manage simple collections. This information is really important to give more flexibility about the way to use that field type.

@Tobion
Copy link
Contributor

Tobion commented Mar 4, 2015

I guess what you are looking for is choice_attr in #12148

@Tobion
Copy link
Contributor

Tobion commented Mar 4, 2015

And have you seen #10071

@ninsuo
Copy link
Author

ninsuo commented Mar 5, 2015

Hello @Tobion

About #10071, this is about adding and removing elements, but not adding elements at a specific position or moving them. We still need to have the collection's field name to do that job.

I do not know if you're reading emails or browsing github, as I edited my original question to give more details I think you'll get the point now.

I assume that moving elements inside a collection is not specific (as well as adding and removing elements). It is really useful to move elements or add elements at a given position. A real use case of collection of form collections for example, when you should manage a set of rules executed in the given order and that can contain an arbitrary number of conditions and actions:

sample

So if you think I do not need the collection's prefix to move a collection even in it contains an arbitrary number of collections (following my question's example), can you give me some hints about the way to rename the descendant form fields?

Thank you

@xabbuh
Copy link
Member

xabbuh commented Apr 6, 2019

I am closing this old issue as it seems nobody is interested in making it happen.

@Guikingone
Copy link
Contributor

As @nicolas-grekas added this issue as a EUFOSSA one, could it be a good idea to reconsider the idea of working on it?

Don't know if @ninsuo is maintaining the lib but for current usages, it could be a good idea to reconsider the solution? 🤔

@ninsuo
Copy link
Author

ninsuo commented Dec 26, 2019

Hello,

No i'm not maintaining it anymore. It needs to be rewritten using the backend more, because putting everything on the client side adds lots of complexity and is resourceful.

But yes, this feature is definitely relevant for complex forms.

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants