Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to customize the prototype #5643

Merged
merged 2 commits into from
Oct 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cookbook/form/form_collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ great, your user can't actually add any new tags yet.
.. _cookbook-form-collections-new-prototype:

Allowing "new" Tags with the "Prototype"
-----------------------------------------
----------------------------------------

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

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

.. seealso::

If you want to customize the HTML code in the prototype, read
:ref:`cookbook-form-custom-prototype`.

To make handling these new tags easier, add an "adder" and a "remover" method
for the tags in the ``Task`` class::

Expand Down
43 changes: 43 additions & 0 deletions cookbook/form/form_customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,49 @@ You can also override the markup for an entire field row using the same method:
<?php echo $view['form']->widget($form) ?>
</div>

.. _cookbook-form-custom-prototype:

How to Customize a Collection Prototype
---------------------------------------

When using a :doc:`collection of forms </cookbook/form/form_collections>`,
the prototype can be overridden with a completely custom prototype by
overriding a block. For example, if your form field is named ``tasks``, you
will be able to change the widget for each task as follows:

.. configuration-block::

.. code-block:: html+jinja

{% form_theme form _self %}

{% block _tasks_entry_widget %}
<tr>
<td>{{ form_widget(task.task) }}</td>
<td>{{ form_widget(task.dueDate) }}</td>
</tr>
{% endblock %}

.. code-block:: html+php

<!-- src/AppBundle/Resources/views/Form/_tasks_entry_widget.html.php -->
<tr>
<td><?php echo $view['form']->widget($form->task) ?></td>
<td><?php echo $view['form']->widget($form->dueDate) ?></td>
</tr>

Not only can you override the rendered widget, but you can also change the
complete form row or the label as well. For the ``tasks`` field given above,
the block names would be the following:

================ =======================
Part of the Form Block Name
================ =======================
``label`` ``_tasks_entry_label``
``widget`` ``_tasks_entry_widget``
``row`` ``_tasks_entry_row``
================ =======================

Other common Customizations
---------------------------

Expand Down