@@ -1265,7 +1265,7 @@ There is no need for a custom template just render the form as usual:
1265
1265
1266
1266
The ``add `` and ``delete `` buttons are rendered as separate ``ButtonType `` form
1267
1267
types and can be customized like a normal form type via the ``live_collection_button_add ``
1268
- and ``live_collection_button_delete `` respectively:
1268
+ and ``live_collection_button_delete `` block prefix respectively:
1269
1269
1270
1270
.. code-block :: twig
1271
1271
@@ -1282,6 +1282,118 @@ and ``live_collection_button_delete`` respectively:
1282
1282
{{ block('button_widget') }}
1283
1283
{% endblock live_collection_button_add_widget %}
1284
1284
1285
+ If you only want to customize some attributes maybe simpler to use the options in the form type:
1286
+
1287
+ // ...
1288
+ ->add('comments', LiveCollectionType::class, [
1289
+ 'entry_type' => CommentFormType::class,
1290
+ 'allow_add' => true,
1291
+ 'allow_delete' => true,
1292
+ 'by_reference' => false,
1293
+ 'label' => false,
1294
+ 'button_delete_options' => [
1295
+ 'label' => 'X',
1296
+ 'attr' => [
1297
+ 'class' => 'btn btn-outline-danger',
1298
+ ],
1299
+ ]
1300
+ ])
1301
+ // ...
1302
+
1303
+ If you want more control over how each row is rendered you can override the blocks
1304
+ related to the ``LiveCollectionType ``. This works the same way as for `the traditional
1305
+ collection type `_, but you should use ``live_collection_* `` and ``live_collection_entry_* ``
1306
+ as prefixes instead.
1307
+
1308
+ For example, let's continue our previous example and customize the rendering of the blog post comments form.
1309
+ By default the add comment button is placed after the comments, let's move it before them.
1310
+
1311
+ .. code-block :: twig
1312
+
1313
+ {%- block live_collection_widget -%}
1314
+ {%- if button_add is defined and not button_add.rendered -%}
1315
+ {{ form_row(button_add) }}
1316
+ {%- endif -%}
1317
+ {{ block('form_widget') }}
1318
+ {%- endblock -%}
1319
+
1320
+ Now add a div around each row:
1321
+
1322
+ .. code-block :: twig
1323
+
1324
+ {%- block live_collection_entry_row -%}
1325
+ <div>
1326
+ {{ block('form_row') }}
1327
+ {%- if button_delete is defined and not button_delete.rendered -%}
1328
+ {{ form_row(button_delete) }}
1329
+ {%- endif -%}
1330
+ </div>
1331
+ {%- endblock -%}
1332
+
1333
+ .. note ::
1334
+
1335
+ Under the hood, ``LiveCollectionType `` adds ``button_add `` and
1336
+ ``button_delete `` fields to the form in a special way. These fields
1337
+ are not added as regular form fields, so they are not part of the form
1338
+ tree, but only the form view. The ``button_add `` is added to the
1339
+ collection view variables and a ``button_delete `` is added to each
1340
+ item view variables.
1341
+
1342
+ As an another example, now let's create a general bootstrap 5 theme for the live
1343
+ collection type, rendering every item in a table row:
1344
+
1345
+ .. code-block :: twig
1346
+
1347
+ {%- block live_collection_widget -%}
1348
+ <table class="table table-borderless form-no-mb">
1349
+ <thead>
1350
+ <tr>
1351
+ {% for child in form|last %}
1352
+ <td>{{ form_label(child) }}</td>
1353
+ {% endfor %}
1354
+ <td></td>
1355
+ </tr>
1356
+ </thead>
1357
+ <tbody>
1358
+ {{ block('form_widget') }}
1359
+ </tbody>
1360
+ </table>
1361
+ {%- if skip_add_button|default(false) is same as false and button_add is defined and not button_add.rendered -%}
1362
+ {{ form_widget(button_add, { label: '+ Add Item', class: 'btn btn-outline-primary' }) }}
1363
+ {%- endif -%}
1364
+ {%- endblock -%}
1365
+
1366
+ {%- block live_collection_entry_row -%}
1367
+ <tr>
1368
+ {% for child in form %}
1369
+ <td>{{- form_row(child, { label: false }) -}}</td>
1370
+ {% endfor %}
1371
+ <td>
1372
+ {{- form_row(button_delete, { label: 'X', attr: { class: 'btn btn-outline-danger' } }) -}}
1373
+ </td>
1374
+ </tr>
1375
+ {%- endblock -%}
1376
+
1377
+ To render the add button later in the template, you can skip rendering it initially with ``skip_add_button ``,
1378
+ then render it manually after:
1379
+
1380
+ .. code-block :: twig
1381
+
1382
+ <table class="table table-borderless form-no-mb">
1383
+ <thead>
1384
+ <tr>
1385
+ <td>Item</td>
1386
+ <td>Priority</td>
1387
+ <td></td>
1388
+ </tr>
1389
+ </thead>
1390
+ <tbody>
1391
+ {{ form_row(form.todoItems, { skip_add_button: true }) }}
1392
+ </tbody>
1393
+ </table>
1394
+
1395
+ {{ form_widget(form.todoItems.vars.button_add, { label: '+ Add Item', class: 'btn btn-outline-primary' }) }}
1396
+
1285
1397
Modifying Nested Object Properties with the "exposed" Option
1286
1398
------------------------------------------------------------
1287
1399
@@ -1768,3 +1880,4 @@ bound to Symfony's BC policy for the moment.
1768
1880
.. _`Symfony UX configured in your app` : https://symfony.com/doc/current/frontend/ux.html
1769
1881
.. _`attributes variable` : https://symfony.com/bundles/ux-twig-component/current/index.html#component-attributes
1770
1882
.. _`CollectionType` : https://symfony.com/doc/current/form/form_collections.html
1883
+ .. _`the traditional collection type` : https://symfony.com/doc/current/form/form_themes.html#fragment-naming-for-collections
0 commit comments