Skip to content

Commit 874f2fd

Browse files
committed
Minor changes
1 parent f69eb01 commit 874f2fd

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

form/form_collections.rst

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -238,19 +238,26 @@ it will receive an *unknown* number of tags. Otherwise, you'll see a
238238

239239
The ``allow_add`` option also makes a ``prototype`` variable available to you.
240240
This "prototype" is a little "template" that contains all the HTML needed to
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:
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
243+
template:
244244

245245
.. code-block:: html+twig
246246

247-
<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>
247+
{# the data-index attribute is required for the JavaScript code below #}
248+
<ul class="tags"
249+
data-index="{{ form.tags|length > 0 ? form.tags|last.vars.name + 1 : 0 }}"
250+
data-prototype="{{ form_widget(form.tags.vars.prototype)|e('html_attr') }}"
251+
></ul>
248252

249253
On the rendered page, the result will look something like this:
250254

251255
.. code-block:: html
252256

253-
<ul class="tags" data-index="0" data-prototype="&lt;div&gt;&lt;label class=&quot; required&quot;&gt;__name__&lt;/label&gt;&lt;div id=&quot;task_tags___name__&quot;&gt;&lt;div&gt;&lt;label for=&quot;task_tags___name___name&quot; class=&quot; required&quot;&gt;Name&lt;/label&gt;&lt;input type=&quot;text&quot; id=&quot;task_tags___name___name&quot; name=&quot;task[tags][__name__][name]&quot; required=&quot;required&quot; maxlength=&quot;255&quot; /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;">
257+
<ul class="tags"
258+
data-index="0"
259+
data-prototype="&lt;div&gt;&lt;label class=&quot; required&quot;&gt;__name__&lt;/label&gt;&lt;div id=&quot;task_tags___name__&quot;&gt;&lt;div&gt;&lt;label for=&quot;task_tags___name___name&quot; class=&quot; required&quot;&gt;Name&lt;/label&gt;&lt;input type=&quot;text&quot; id=&quot;task_tags___name___name&quot; name=&quot;task[tags][__name__][name]&quot; required=&quot;required&quot; maxlength=&quot;255&quot; /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;"
260+
></ul>
254261

255262
Now add a button to dynamically add a new tag:
256263

@@ -282,16 +289,16 @@ Now add a button to dynamically add a new tag:
282289
and you need to adjust the following JavaScript accordingly.
283290

284291
Now add some JavaScript to read this attribute and dynamically add new tag forms
285-
when the user clicks the "Add a tag" link.
286-
287-
Add a ``<script>`` tag somewhere on your page to include the required
288-
functionality with JavaScript:
292+
when the user clicks the "Add a tag" link. Add a ``<script>`` tag somewhere
293+
on your page to include the required functionality with JavaScript:
289294

290295
.. code-block:: javascript
291296
292297
document
293298
.querySelectorAll('.add_item_link')
294-
.forEach(btn => btn.addEventListener("click", addFormToCollection));
299+
.forEach(btn => {
300+
btn.addEventListener("click", addFormToCollection)
301+
});
295302
296303
The ``addFormToCollection()`` function's job will be to use the ``data-prototype``
297304
attribute to dynamically add a new form when this link is clicked. The ``data-prototype``
@@ -304,17 +311,17 @@ you'll replace with a unique, incrementing number (e.g. ``task[tags][3][name]``)
304311
const addFormToCollection = (e) => {
305312
const collectionHolder = document.querySelector('.' + e.currentTarget.dataset.collectionHolderClass);
306313
307-
const element = document.createElement('li');
314+
const item = document.createElement('li');
308315
309-
element.innerHTML = collectionHolder
316+
item.innerHTML = collectionHolder
310317
.dataset
311318
.prototype
312319
.replace(
313320
/__name__/g,
314321
collectionHolder.dataset.index
315322
);
316323
317-
collectionHolder.appendChild(element);
324+
collectionHolder.appendChild(item);
318325
319326
collectionHolder.dataset.index++;
320327
};
@@ -530,34 +537,35 @@ First, add a "delete this tag" link to each tag form:
530537

531538
.. code-block:: javascript
532539
533-
const tags = document.querySelectorAll('ul.tags li')
534-
tags.forEach((tag) => {
535-
addTagFormDeleteLink(tag)
536-
})
540+
document
541+
.querySelectorAll('ul.tags li')
542+
.forEach((tag) => {
543+
addTagFormDeleteLink(tag)
544+
})
537545
538546
// ... the rest of the block from above
539547
540-
function addFormToCollection() {
548+
const addFormToCollection = (e) => {
541549
// ...
542550
543551
// add a delete link to the new form
544-
addTagFormDeleteLink(element);
552+
addTagFormDeleteLink(item);
545553
}
546554
547555
The ``addTagFormDeleteLink()`` function will look something like this:
548556

549557
.. code-block:: javascript
550558
551-
const addTagFormDeleteLink = (element) => {
559+
const addTagFormDeleteLink = (item) => {
552560
const removeFormButton = document.createElement('button');
553561
removeFormButton.innerText = 'Delete this tag';
554562
555-
element.append(removeFormButton);
563+
item.append(removeFormButton);
556564
557565
removeFormButton.addEventListener('click', (e) => {
558566
e.preventDefault();
559567
// remove the li for the tag form
560-
element.remove();
568+
item.remove();
561569
});
562570
}
563571

0 commit comments

Comments
 (0)