Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

embedded forms: missing data-index attribute? #42870

Answered by chapterjason
ghertko asked this question in Q&A
Discussion options

In the docs about embedded forms is a Twig-Snippet for the ul that holds new sub-form elements. In HTML output it seems that the data-index attribute is missing. So I assume it should be added in Twig directly?! If I do that, everything works fine. Need the docs to be adjusted?

You must be logged in to vote

@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 …

Replies: 1 comment · 3 replies

Comment options

@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="&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;">
+    <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;">
You must be logged in to vote
3 replies
@ghertko
Comment options

Thanks for this fix!

@stof
Comment options

stof Sep 3, 2021
Collaborator

@chapterjason can you send a PR fixing the doc ?

@chapterjason
Comment options

@stof Sure. :)

Answer selected by ghertko
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.