@@ -307,10 +307,66 @@ Fragment Naming for Collections
307307...............................
308308
309309When 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 %}
314370
315371 Template Fragment Inheritance
316372.............................
@@ -341,6 +397,8 @@ for any overridden form blocks:
341397
342398.. code-block :: html+twig
343399
400+ {% extends 'base.html.twig' %}
401+
344402 {% form_theme form _self %}
345403
346404 {# this overrides the widget of any field of type integer, but only in the
@@ -359,12 +417,16 @@ for any overridden form blocks:
359417 </div>
360418 {% endblock %}
361419
362-
363420 {# ... render the form ... #}
364421
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.
368430
369431Creating a Form Theme in a Separate Template
370432~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments