Skip to content

Shortening the next paragraph #13443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions form/form_collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ that Task, right inside the same form.
including the ``ManyToMany`` association mapping definition on the Task's
``tags`` property.

Let's start by creating a simple ``Task`` entity::
Let's start by creating a ``Task`` entity::

// src/AppBundle/Entity/Task.php
namespace AppBundle\Entity;
Expand Down Expand Up @@ -157,8 +157,7 @@ In your controller, you'll create a new form from the ``TaskType``::
{
$task = new Task();

// dummy code - this is here just so that the Task has some tags
// otherwise, this isn't an interesting example
// dummy code - add some tags to the task to play with
$tag1 = new Tag();
$tag1->setName('tag1');
$task->getTags()->add($tag1);
Expand All @@ -172,7 +171,7 @@ In your controller, you'll create a new form from the ``TaskType``::
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
// ... maybe do some form processing, like saving the Task and Tag objects
// ... do your form processing, like saving the Task and Tag entities
}

return $this->render('task/new.html.twig', [
Expand All @@ -181,11 +180,8 @@ In your controller, you'll create a new form from the ``TaskType``::
}
}

The corresponding template is now able to render both the ``description``
field for the task form as well as all the ``TagType`` forms for any tags
that are already related to this ``Task``. In the above controller, I added
some dummy code so that you can see this in action (since a ``Task`` has
zero tags when first created).
In the template, we need to iterate over the existing ``TagType`` forms
to render them:

.. code-block:: html+twig

Expand All @@ -194,20 +190,15 @@ zero tags when first created).
{# ... #}

{{ form_start(form) }}
{# render the task's only field: description #}
{{ form_row(form.description) }}

<h3>Tags</h3>
<ul class="tags">
{# iterate over each existing tag and render its only field: name #}
{% for tag in form.tags %}
<li>{{ form_row(tag.name) }}</li>
{% endfor %}
</ul>
{{ form_end(form) }}

{# ... #}

When the user submits the form, the submitted data for the ``tags`` field are
used to construct an ``ArrayCollection`` of ``Tag`` objects, which is then set
on the ``tag`` field of the ``Task`` instance.
Expand Down