@@ -307,10 +307,66 @@ Fragment Naming for Collections
307
307
...............................
308
308
309
309
When using a :doc: `collection of forms </form/form_collections >`, the fragment
310
- of each collection item follows the pattern ``_field-name_entry_part ``. For
311
- example, if your form field is named ``tasks ``, the fragment for each task will
312
- be named ``_tasks_entry `` (``_tasks_entry_row ``, ``_tasks_entry_label ``,
313
- ``_tasks_entry_widget ``, ``_tasks_entry_error ``)
310
+ of each collection item follows a predefined pattern. For example, consider the
311
+ following complex example where a ``TaskManagerType `` has a collection of
312
+ ``TaskListType `` which in turn has a collection of ``TaskType ``::
313
+
314
+ class TaskManagerType extends AbstractType
315
+ {
316
+ public function buildForm(FormBuilderInterface $builder, array $options = array())
317
+ {
318
+ // ...
319
+ $builder->add('taskLists', CollectionType::class, array(
320
+ 'entry_type' => TaskListType::class,
321
+ 'block_name' => 'task_lists',
322
+ ));
323
+ }
324
+ }
325
+
326
+ class TaskListType extends AbstractType
327
+ {
328
+ public function buildForm(FormBuilderInterface $builder, array $options = array())
329
+ {
330
+ // ...
331
+ $builder->add('tasks', CollectionType::class, array(
332
+ 'entry_type' => TaskType::class,
333
+ ));
334
+ }
335
+ }
336
+
337
+ class TaskType
338
+ {
339
+ public function buildForm(FormBuilderInterface $builder, array $options = array())
340
+ {
341
+ $builder->add('name');
342
+ // ...
343
+ }
344
+ }
345
+
346
+ Then you get all the following customizable blocks (where ``* `` can be replaced
347
+ by ``row ``, ``widget ``, ``label ``, or ``help ``):
348
+
349
+ .. code-block :: twig
350
+
351
+ {% block _task_manager_task_lists_* %}
352
+ {# the collection field of TaskManager #}
353
+ {% endblock %}
354
+
355
+ {% block _task_manager_task_lists_entry_* %}
356
+ {# the inner TaskListType #}
357
+ {% endblock %}
358
+
359
+ {% block _task_manager_task_lists_entry_tasks_* %}
360
+ {# the collection field of TaskListType #}
361
+ {% endblock %}
362
+
363
+ {% block _task_manager_task_lists_entry_tasks_entry_* %}
364
+ {# the inner TaskType #}
365
+ {% endblock %}
366
+
367
+ {% block _task_manager_task_lists_entry_tasks_entry_name_* %}
368
+ {# the field of TaskType #}
369
+ {% endblock %}
314
370
315
371
Template Fragment Inheritance
316
372
.............................
@@ -341,6 +397,8 @@ for any overridden form blocks:
341
397
342
398
.. code-block :: html+twig
343
399
400
+ {% extends 'base.html.twig' %}
401
+
344
402
{% form_theme form _self %}
345
403
346
404
{# this overrides the widget of any field of type integer, but only in the
@@ -359,12 +417,16 @@ for any overridden form blocks:
359
417
</div>
360
418
{% endblock %}
361
419
362
-
363
420
{# ... render the form ... #}
364
421
365
- The disadvantage of this method is that the customized form blocks can't be
366
- reused when rendering other forms in other templates. If that's what you need,
367
- create a form theme in a separate template as explained in the next section.
422
+ The main disadvantage of this method is that it only works if your template
423
+ extends another (``'base.html.twig' `` in the previous example). If your template
424
+ does not, you must point ``form_theme `` to a separate template, as explained in
425
+ the next section.
426
+
427
+ Another disadvantage is that the customized form blocks can't be reused when
428
+ rendering other forms in other templates. If that's what you need, create a form
429
+ theme in a separate template as explained in the next section.
368
430
369
431
Creating a Form Theme in a Separate Template
370
432
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments