-
-
Couldn't load subscription status.
- Fork 5.3k
[Form] Added doc for collection prototype #572
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
Conversation
book/forms.rst Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not always true (i.e. when some items have been deleted)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I change "number of items" by "number of rows" ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the phrase to "you might have have to change this to be coherent with your row ordering."
| Hey Marc! I'm really glad that someone has taken this on - with a coherent example, this is a really fantastic feature (and the collection type is totally undocumented). I still need to take a look at this, but I do know that I think we should move this into the collection form type reference (http://symfony.com/doc/current/reference/forms/types/collection.html). And actually, that page needs everything, so if there are any details you can fill in there (I haven't really used the collection type yet), that would be a huge help! Thanks! |
| What about documentation on collections and prototype themeing. If I understood it more, I'd make a cookbook article. Or if I figure it out today, I'll go about it. I just think prototypes should mimic the collection widget entirely (which I'm sure it does, but I'm not sure.. I haven't looked into it) ie. adding remove buttons and add buttons to the template instead of being forced to use javascript after dom load. |
| Okay, I go this far: And I just realized that the form_row(prototype) doesn't mimic the collection at all. Is there a better way to do this? |
| well, you are rendering the children by hand. You need to do the same for the prototype if you want to apply the same rendering |
| @jstout24 Yes, we need to document the Goals:
Maybe we're not so far off from this - @marcw may already be really close, but it wasn't clear enough for me to understand (for example, I didn't understand the problem with "row ordering"). Thanks! |
| I don't have much time to work on it these days but will try to provide a cookbook entry for this one. |
| It could really be great to implement some kind of contact editor with nested collection too ass seen here http://knockoutjs.com/examples/contactsEditor.html |
| +1 on this. We definitly must have a cookbook to know how to handle this correctly. |
| I've been playing with collections and the prototype attribute (so far so good). I'd be willing to write some documentation once I wrap my head around it some more. I've successfully added to the collection now I'm tackling deleting rows. As far as I can see there are two options:
What would be the preferred approach? Update: After thinking about this a bit more I think a deleted flag would be a better idea. From a front end point of view you could reduce the opacity of the row on delete and then commit the change in the controller. This would also add the possibility of an undo state on the front end. |
| Hey @leevigraham! I could be wrong since I haven't really dove into this stuff, but I believe that the server-side of the "remove" problem is handled automatically, once you have the And if I'm wrong, someone will certainly correct me :). Btw, I would very much appreciate it if you would take a shot at this - it's not so hard, just a matter of getting it going and improving it until everyone is happy. Cheers! |
| Ryan, as the collection type is broken with some Doctrine relations, you might need to do some more work to handle the deletion. |
| I have been working on this. The javascript i implemented is working perfectly. I had to write a controller helper method to successfully delete collection members and ignore empty collection members. This works great when there's only one member of each collection submitted. The problem is when I submit multiples to any collection I get this in validation: |
| re: my above post: adding allow_add was the missing piece that I didnt have set in my formtype. once I set the allow_add value it added the extra fields perfectly. This makes total sense its just not well documented. |
| @jessegreathouse adding orphanRemoval=true to your entity annotations will automatically delete orphaned entities. |
| tnx leevigraham ! |
| I'm still planning on contributing to this doc when I ge time but in the mean time here's the JS I'm using to add / delete collection forms: |
| Having a collection documentation would be a huge relief.. The above js from leevigraham works for normal cases, though in the case collections of collections the second 'prototype' data is not expanded and the add/remove buttons are not added. This function should be called recursively after $(this).before($p); . That's mostly what I do in my code, though I'm now stuck with a new problem as the replace() function will replace the second '$$name$$' as well. For instance: div="timetable_trips_$$name$$_stoptimes_$$name$$"(Timetable contains a collection of 'Trips' that contain a collection of 'stopTimes') I guess I will need to have some kind of parsing of the prototype data instead of a simple replace. Another question: why isn't the data-prototype attribute value html encoded? is that safe to have unencoded stuff there? It contains quotes, I guess some browsers might not parse this correctly.. To be on the safe side I always encode it using the '|e' twig function like this: {% block collection_widget %} {% spaceless %} <div class="collection"> {% if prototype is defined %} {% set attr = attr|merge({'data-prototype': form_widget(prototype)|e }) %} {% endif %} </div> {% endspaceless %} {% endblock collection_widget %} |
| it is possible to affect the prototype 'shape' ? |
Based on PR #1724