@@ -238,25 +238,26 @@ it will receive an *unknown* number of tags. Otherwise, you'll see a
238238
239239The ``allow_add `` option also makes a ``prototype `` variable available to you.
240240This "prototype" is a little "template" that contains all the HTML needed to
241- dynamically create any new "tag" forms with JavaScript. To render the prototype, add
242- the following ``data-prototype `` attribute to the existing ``<ul> `` in your template:
241+ dynamically create any new "tag" forms with JavaScript. Now add the following
242+ ``data-index `` (containing the current "form row" number for the following JavaScript)
243+ and ``data-prototype `` attribute to the existing ``<ul> `` in your template:
243244
244245.. code-block :: html+twig
245246
246247 <ul class="tags" data-index="{{ form.tags|length > 0 ? form.tags|last.vars.name + 1 : 0 }}" data-prototype="{{ form_widget(form.tags.vars.prototype)|e('html_attr') }}"></ul>
247248
248- Now add a button just next to the ``<ul> `` to dynamically add a new tag:
249-
250- .. code-block :: html+twig
251-
252- <button type="button" class="add_item_link" data-collection-holder-class="tags">Add a tag</button>
253-
254249On the rendered page, the result will look something like this:
255250
256251.. code-block :: html
257252
258253 <ul class =" tags" data-index =" 0" data-prototype =" < ; div> ;< ; label class=" ; required" ;> ; __name__< ; /label> ;< ; div id=" ; task_tags___name__" ;> ;< ; div> ;< ; label for=" ; task_tags___name___name" ; class=" ; required" ;> ; Name< ; /label> ;< ; input type=" ; text" ; id=" ; task_tags___name___name" ; name=" ; task[tags][__name__][name]" ; required=" ; required" ; maxlength=" ; 255" ; /> ;< ; /div> ;< ; /div> ;< ; /div> ; " >
259254
255+ Now add a button to dynamically add a new tag:
256+
257+ .. code-block :: html+twig
258+
259+ <button type="button" class="add_item_link" data-collection-holder-class="tags">Add a tag</button>
260+
260261.. seealso ::
261262
262263 If you want to customize the HTML code in the prototype, see
@@ -265,7 +266,7 @@ On the rendered page, the result will look something like this:
265266.. tip ::
266267
267268 The ``form.tags.vars.prototype `` is a form element that looks and feels just
268- like the individual ``form_widget(tag) `` elements inside your ``for `` loop.
269+ like the individual ``form_widget(tag.* ) `` elements inside your ``for `` loop.
269270 This means that you can call ``form_widget() ``, ``form_row() `` or ``form_label() ``
270271 on it. You could even choose to render only one of its fields (e.g. the
271272 ``name `` field):
@@ -303,17 +304,17 @@ you'll replace with a unique, incrementing number (e.g. ``task[tags][3][name]``)
303304 const addFormToCollection = (e ) => {
304305 const collectionHolder = document .querySelector (' .' + e .currentTarget .dataset .collectionHolderClass );
305306
306- const item = document .createElement (' li' );
307+ const element = document .createElement (' li' );
307308
308- item .innerHTML = collectionHolder
309+ element .innerHTML = collectionHolder
309310 .dataset
310311 .prototype
311312 .replace (
312313 / __name__/ g ,
313314 collectionHolder .dataset .index
314315 );
315316
316- collectionHolder .appendChild (item );
317+ collectionHolder .appendChild (element );
317318
318319 collectionHolder .dataset .index ++ ;
319320 };
@@ -540,24 +541,23 @@ First, add a "delete this tag" link to each tag form:
540541 // ...
541542
542543 // add a delete link to the new form
543- addTagFormDeleteLink (item );
544+ addTagFormDeleteLink (element );
544545 }
545546
546547 The ``addTagFormDeleteLink() `` function will look something like this:
547548
548549.. code-block :: javascript
549550
550- const addTagFormDeleteLink = (tagFormLi ) => {
551- const removeFormButton = document .createElement (' button' )
552- removeFormButton .classList
553- removeFormButton .innerText = ' Delete this tag'
551+ const addTagFormDeleteLink = (element ) => {
552+ const removeFormButton = document .createElement (' button' );
553+ removeFormButton .innerText = ' Delete this tag' ;
554554
555- tagFormLi .append (removeFormButton);
555+ element .append (removeFormButton);
556556
557557 removeFormButton .addEventListener (' click' , (e ) => {
558- e .preventDefault ()
558+ e .preventDefault ();
559559 // remove the li for the tag form
560- tagFormLi .remove ();
560+ element .remove ();
561561 });
562562 }
563563
@@ -652,7 +652,6 @@ the relationship between the removed ``Tag`` and ``Task`` object.
652652 the `symfony-collection `_ package based on `jQuery `_ for the rest of browsers.
653653
654654.. _`Owning Side and Inverse Side` : https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/unitofwork-associations.html
655- .. _`jQuery` : http://jquery.com/
656655.. _`JSFiddle` : https://jsfiddle.net/ey8ozh6n/
657656.. _`@a2lix/symfony-collection` : https://github.com/a2lix/symfony-collection
658657.. _`symfony-collection` : https://github.com/ninsuo/symfony-collection
0 commit comments