@@ -19,8 +19,7 @@ that Task, right inside the same form.
19
19
including the ``ManyToMany `` association mapping definition on the Task's
20
20
``tags `` property.
21
21
22
- First, suppose that each ``Task `` belongs to multiple ``Tag `` objects. Start
23
- by creating a simple ``Task `` class::
22
+ Let's start by creating a ``Task `` entity::
24
23
25
24
// src/Entity/Task.php
26
25
namespace App\Entity;
@@ -30,7 +29,6 @@ by creating a simple ``Task`` class::
30
29
class Task
31
30
{
32
31
protected $description;
33
-
34
32
protected $tags;
35
33
36
34
public function __construct()
@@ -159,8 +157,8 @@ In your controller, you'll create a new form from the ``TaskType``::
159
157
{
160
158
$task = new Task();
161
159
162
- // dummy code - this is here just so that the Task has some tags
163
- // otherwise, this isn't an interesting example
160
+ // dummy code - add some example tags to the task
161
+ // ( otherwise, the template will render an empty list of tags)
164
162
$tag1 = new Tag();
165
163
$tag1->setName('tag1');
166
164
$task->getTags()->add($tag1);
@@ -174,7 +172,7 @@ In your controller, you'll create a new form from the ``TaskType``::
174
172
$form->handleRequest($request);
175
173
176
174
if ($form->isSubmitted() && $form->isValid()) {
177
- // ... maybe do some form processing, like saving the Task and Tag objects
175
+ // ... do your form processing, like saving the Task and Tag entities
178
176
}
179
177
180
178
return $this->render('task/new.html.twig', [
@@ -183,11 +181,8 @@ In your controller, you'll create a new form from the ``TaskType``::
183
181
}
184
182
}
185
183
186
- The corresponding template is now able to render both the ``description ``
187
- field for the task form as well as all the ``TagType `` forms for any tags
188
- that are already related to this ``Task ``. In the above controller, I added
189
- some dummy code so that you can see this in action (since a ``Task `` has
190
- zero tags when first created).
184
+ In the template, you can now iterate over the existing ``TagType `` forms
185
+ to render them:
191
186
192
187
.. code-block :: html+twig
193
188
@@ -196,12 +191,10 @@ zero tags when first created).
196
191
{# ... #}
197
192
198
193
{{ form_start(form) }}
199
- {# render the task's only field: description #}
200
194
{{ form_row(form.description) }}
201
195
202
196
<h3>Tags</h3>
203
197
<ul class="tags">
204
- {# iterate over each existing tag and render its only field: name #}
205
198
{% for tag in form.tags %}
206
199
<li>{{ form_row(tag.name) }}</li>
207
200
{% endfor %}
0 commit comments