@@ -262,16 +262,25 @@ add the ``allow_add`` option to your collection field::
262
262
]);
263
263
}
264
264
265
- In addition to telling the field to accept any number of submitted objects, the
266
- `` allow_add `` also makes a * "prototype" * variable available to you. This "prototype"
267
- is a little "template" that contains all the HTML to be able to render any
268
- new "tag" forms. To render it, make the following change to your template:
265
+ The `` allow_add `` option also makes a `` prototype `` variable available to you.
266
+ This "prototype" is a little "template" that contains all the HTML needed to
267
+ dynamically create any new "tag" forms with JavaScript. To render the prototype, add
268
+ the following `` data-prototype `` attribute to the existing `` <ul> `` in your template:
269
269
270
270
.. code-block :: html+twig
271
271
272
272
<ul class="tags" data-prototype="{{ form_widget(form.tags.vars.prototype)|e('html_attr') }}">
273
- ...
274
- </ul>
273
+
274
+ On the rendered page, the result will look something like this:
275
+
276
+ .. code-block :: html
277
+
278
+ <ul class =" tags" 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> ; " >
279
+
280
+ .. seealso ::
281
+
282
+ If you want to customize the HTML code in the prototype, see
283
+ :ref: `form-custom-prototype `.
275
284
276
285
.. note ::
277
286
@@ -291,12 +300,6 @@ new "tag" forms. To render it, make the following change to your template:
291
300
292
301
{{ form_widget(form.tags.vars.prototype.name)|e }}
293
302
294
- On the rendered page, the result will look something like this:
295
-
296
- .. code-block :: html
297
-
298
- <ul class =" tags" 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> ; " >
299
-
300
303
The goal of this section will be to use JavaScript to read this attribute
301
304
and dynamically add new tag forms when the user clicks a "Add a tag" link.
302
305
This example uses jQuery and assumes you have it included somewhere on your page.
@@ -381,11 +384,6 @@ into new ``Tag`` objects and added to the ``tags`` property of the ``Task`` obje
381
384
382
385
You can find a working example in this `JSFiddle `_.
383
386
384
- .. seealso ::
385
-
386
- If you want to customize the HTML code in the prototype, read
387
- :ref: `form-custom-prototype `.
388
-
389
387
To make handling these new tags easier, add an "adder" and a "remover" method
390
388
for the tags in the ``Task `` class::
391
389
0 commit comments