embedded forms: missing data-index
attribute?
#42870
-
In the docs about embedded forms is a Twig-Snippet for the |
Beta Was this translation helpful? Give feedback.
Answered by
chapterjason
Sep 3, 2021
Replies: 1 comment · 3 replies
-
@ghertko You are right, there is something mixed up. https://github.com/symfony/symfony-docs/blob/5.4/form/form_collections.rst .. code-block:: html+twig
- <ul class="tags" data-prototype="{{ form_widget(form.tags.vars.prototype)|e('html_attr') }}"></ul>
+ <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>
Now add a button just next to the ``<ul>`` to dynamically add a new tag:
.. code-block:: html+twig
<button type="button" class="add_item_link" data-collection-holder-class="tags">Add a tag</button>
On the rendered page, the result will look something like this:
.. code-block:: html
- <ul class="tags" data-index="{{ form.tags|length > 0 ? form.tags|last.vars.name + 1 : 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>">
+ <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>">
|
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
ghertko
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ghertko You are right, there is something mixed up.
https://github.com/symfony/symfony-docs/blob/5.4/form/form_collections.rst