@@ -156,65 +156,64 @@ Using jQuery, a simple example might look like this. If you're rendering
156156your collection fields all at once (e.g. ``form_row(form.emails) ``), then
157157things are even easier because the ``data-prototype `` attribute is rendered
158158automatically for you (with a slight difference - see note below) and all
159- you need is the JavaScript:
159+ you need is this JavaScript code:
160+
161+ .. code-block :: javascript
162+
163+ // add-collection-widget.js
164+ jQuery (document ).ready (function () {
165+ jQuery (' .add-another-collection-widget' ).click (function (e ) {
166+ e .preventDefault ();
167+ var list = jQuery (jQuery (this ).attr (' data-list' ));
168+ // Try to find the counter of the list
169+ var counter = list .data (' widget-counter' ) | list .children ().length ;
170+ // If the counter does not exist, use the length of the list
171+ if (! counter) { counter = list .children ().length ; }
172+
173+ // grab the prototype template
174+ var newWidget = list .attr (' data-prototype' );
175+ // replace the "__name__" used in the id and name of the prototype
176+ // with a number that's unique to your emails
177+ // end name attribute looks like name="contact[emails][2]"
178+ newWidget = newWidget .replace (/ __name__/ g , counter);
179+ // Increase the counter
180+ counter++ ;
181+ // And store it, the length cannot be used if deleting widgets is allowed
182+ list .data (' widget-counter' , counter);
183+
184+ // create a new list element and add it to the list
185+ var newElem = jQuery (list .attr (' data-widget-tags' )).html (newWidget);
186+ newElem .appendTo (list);
187+ });
188+ });
160189
190+ And update the template as follows:
161191
162- .. configuration -block ::
192+ .. code -block :: html+twig
163193
164- .. code-block :: html+twig
194+ {{ form_start(form) }}
195+ {# ... #}
165196
166- {{ form_start(form) }}
167- {# ... #}
168-
169- {# store the prototype on the data-prototype attribute #}
170- <ul id="email-fields-list"
171- data-prototype="{{ form_widget(form.emails.vars.prototype)|e }}"
172- data-widget-tags="{{ '<li></li>'|e }}">
173- {% for emailField in form.emails %}
174- <li>
175- {{ form_errors(emailField) }}
176- {{ form_widget(emailField) }}
177- </li>
178- {% endfor %}
179- </ul>
180-
181- <a href="#"
182- class="add-another-collection-widget"
183- data-list="#email-field-list>Add another email</a>
184-
185- {# ... #}
186- {{ form_end(form) }}
187-
188- <script src="add-collection-widget.js"></script>
189-
190- .. code-block :: javascript
191-
192- // add-collection-widget.js
193- jQuery (document ).ready (function () {
194- jQuery (' .add-another-collection-widget' ).click (function (e ) {
195- e .preventDefault ();
196- var list = jQuery (jQuery (this ).attr (' data-list' ));
197- // Try to find the counter of the list
198- var counter = list .data (' widget-counter' );
199- // If the counter does not exist, use the length of the list
200- if (! counter) { counter = list .children ().length ; }
201-
202- // grab the prototype template
203- var newWidget = list .attr (' data-prototype' );
204- // replace the "__name__" used in the id and name of the prototype
205- // with a number that's unique to your emails
206- // end name attribute looks like name="contact[emails][2]"
207- newWidget = newWidget .replace (/ __name__/ g , counter);
208- // Increase the counter
209- counter++ ;
210- // And store it, the length cannot be used if deleting widgets is allowed
211- list .data (' widget-counter' , counter);
212-
213- // create a new list element and add it to the list
214- var newElem = jQuery (list .attr (' data-widget-tags' )).html (newWidget);
215- newElem .appendTo (list);
216- });
217- });
197+ {# store the prototype on the data-prototype attribute #}
198+ <ul id="email-fields-list"
199+ data-prototype="{{ form_widget(form.emails.vars.prototype)|e }}"
200+ data-widget-tags="{{ '<li></li>'|e }}">
201+ {% for emailField in form.emails %}
202+ <li>
203+ {{ form_errors(emailField) }}
204+ {{ form_widget(emailField) }}
205+ </li>
206+ {% endfor %}
207+ </ul>
208+
209+ <a href="#"
210+ class="add-another-collection-widget"
211+ data-list="#email-field-list>Add another email</a>
212+
213+ {# ... #}
214+ {{ form_end(form) }}
215+
216+ <script src="add-collection-widget.js"></script>
218217
219218.. tip ::
220219
0 commit comments