@@ -159,8 +159,7 @@ In your controller, you'll create a new form from the ``TaskType``::
159
159
{
160
160
$task = new Task();
161
161
162
- // dummy code - this is here just so that the Task has some tags
163
- // otherwise, this isn't an interesting example
162
+ // dummy code - add some tags to the task to play with
164
163
$tag1 = new Tag();
165
164
$tag1->setName('tag1');
166
165
$task->getTags()->add($tag1);
@@ -174,7 +173,7 @@ In your controller, you'll create a new form from the ``TaskType``::
174
173
$form->handleRequest($request);
175
174
176
175
if ($form->isSubmitted() && $form->isValid()) {
177
- // ... maybe do some form processing, like saving the Task and Tag objects
176
+ // ... do your form processing, like saving the Task and Tag entities
178
177
}
179
178
180
179
return $this->render('task/new.html.twig', [
@@ -183,11 +182,8 @@ In your controller, you'll create a new form from the ``TaskType``::
183
182
}
184
183
}
185
184
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).
185
+ In the template, we need to iterate over the existing ``TagType `` forms
186
+ to render them:
191
187
192
188
.. code-block :: html+twig
193
189
@@ -196,20 +192,15 @@ zero tags when first created).
196
192
{# ... #}
197
193
198
194
{{ form_start(form) }}
199
- {# render the task's only field: description #}
200
195
{{ form_row(form.description) }}
201
-
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 %}
208
201
</ul>
209
202
{{ form_end(form) }}
210
203
211
- {# ... #}
212
-
213
204
When the user submits the form, the submitted data for the ``tags `` field are
214
205
used to construct an ``ArrayCollection `` of ``Tag `` objects, which is then set
215
206
on the ``tag `` field of the ``Task `` instance.
0 commit comments