Skip to content

Commit 9736d39

Browse files
committed
minor #10953 Improve add to collection JS example (welcoMattic)
This PR was merged into the 4.2 branch. Discussion ---------- Improve add to collection JS example - Improve `data-*` attributes naming (more precise) - Add missing `data-widget-counter` in Twig code block <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/roadmap for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `master` for features of unreleased versions). --> Commits ------- 907703b Improve add to collection JS exemple
2 parents 0817f94 + 907703b commit 9736d39

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

reference/forms/types/collection.rst

+4-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ you need is this JavaScript code:
149149
// add-collection-widget.js
150150
jQuery(document).ready(function () {
151151
jQuery('.add-another-collection-widget').click(function (e) {
152-
var list = jQuery(jQuery(this).attr('data-list'));
152+
var list = jQuery(jQuery(this).attr('data-list-selector'));
153153
// Try to find the counter of the list or use the length of the list
154154
var counter = list.data('widget-counter') | list.children().length;
155155
@@ -180,7 +180,8 @@ And update the template as follows:
180180
{# store the prototype on the data-prototype attribute #}
181181
<ul id="email-fields-list"
182182
data-prototype="{{ form_widget(form.emails.vars.prototype)|e }}"
183-
data-widget-tags="{{ '<li></li>'|e }}">
183+
data-widget-tags="{{ '<li></li>'|e }}"
184+
data-widget-counter="{{ form.children|length }}">
184185
{% for emailField in form.emails %}
185186
<li>
186187
{{ form_errors(emailField) }}
@@ -191,7 +192,7 @@ And update the template as follows:
191192
192193
<button type="button"
193194
class="add-another-collection-widget"
194-
data-list="#email-fields-list">Add another email</button>
195+
data-list-selector="#email-fields-list">Add another email</button>
195196

196197
{# ... #}
197198
{{ form_end(form) }}

0 commit comments

Comments
 (0)