Skip to content

Commit 504a8f9

Browse files
committed
minor #5643 Document how to customize the prototype (daFish, WouterJ)
This PR was merged into the 2.3 branch. Discussion ---------- Document how to customize the prototype Finishes #4629 and #4797 | Q | A | --- | --- | Doc fix? | no | New docs? | yes | Applies to | all | Fixed tickets | - Commits ------- ce8110c Improve docs on customizing prototype rendering 051a23f document how to render custom collection prototypes
2 parents b90bf96 + ce8110c commit 504a8f9

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

cookbook/form/form_collections.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ great, your user can't actually add any new tags yet.
262262
.. _cookbook-form-collections-new-prototype:
263263

264264
Allowing "new" Tags with the "Prototype"
265-
-----------------------------------------
265+
----------------------------------------
266266

267267
Allowing the user to dynamically add new tags means that you'll need to
268268
use some JavaScript. Previously you added two tags to your form in the controller.
@@ -417,6 +417,11 @@ into new ``Tag`` objects and added to the ``tags`` property of the ``Task`` obje
417417

418418
You can find a working example in this `JSFiddle`_.
419419

420+
.. seealso::
421+
422+
If you want to customize the HTML code in the prototype, read
423+
:ref:`cookbook-form-custom-prototype`.
424+
420425
To make handling these new tags easier, add an "adder" and a "remover" method
421426
for the tags in the ``Task`` class::
422427

cookbook/form/form_customization.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,49 @@ You can also override the markup for an entire field row using the same method:
734734
<?php echo $view['form']->widget($form) ?>
735735
</div>
736736

737+
.. _cookbook-form-custom-prototype:
738+
739+
How to Customize a Collection Prototype
740+
---------------------------------------
741+
742+
When using a :doc:`collection of forms </cookbook/form/form_collections>`,
743+
the prototype can be overridden with a completely custom prototype by
744+
overriding a block. For example, if your form field is named ``tasks``, you
745+
will be able to change the widget for each task as follows:
746+
747+
.. configuration-block::
748+
749+
.. code-block:: html+jinja
750+
751+
{% form_theme form _self %}
752+
753+
{% block _tasks_entry_widget %}
754+
<tr>
755+
<td>{{ form_widget(task.task) }}</td>
756+
<td>{{ form_widget(task.dueDate) }}</td>
757+
</tr>
758+
{% endblock %}
759+
760+
.. code-block:: html+php
761+
762+
<!-- src/AppBundle/Resources/views/Form/_tasks_entry_widget.html.php -->
763+
<tr>
764+
<td><?php echo $view['form']->widget($form->task) ?></td>
765+
<td><?php echo $view['form']->widget($form->dueDate) ?></td>
766+
</tr>
767+
768+
Not only can you override the rendered widget, but you can also change the
769+
complete form row or the label as well. For the ``tasks`` field given above,
770+
the block names would be the following:
771+
772+
================ =======================
773+
Part of the Form Block Name
774+
================ =======================
775+
``label`` ``_tasks_entry_label``
776+
``widget`` ``_tasks_entry_widget``
777+
``row`` ``_tasks_entry_row``
778+
================ =======================
779+
737780
Other common Customizations
738781
---------------------------
739782

0 commit comments

Comments
 (0)