Skip to content

Finish #1998 #4629

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

Closed
wants to merge 1 commit into from
Closed
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
70 changes: 70 additions & 0 deletions cookbook/form/form_collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -729,5 +729,75 @@ the relationship between the removed ``Tag`` and ``Task`` object.
updated (whether you're adding new tags or removing existing tags) on
each Tag object itself.

.. _cookbook-form-collections-custom-prototype:

Rendering a Custom Prototype
----------------------------

Most of the time the provided prototype will be sufficient for your needs
and does not need to be changed. But if you are in the situation were you
need to have a complete custom prototype, you can render it yourself.

The Form component automatically looks for a block whose name follows a certain
schema to decide how to render each entry of the form type collection. 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``
================ =======================

Then, you only have to ensure to render the collection type's ``data-prototype``
property with the proper prototype so that new entries will be rendered the
same way as existing ones:

.. configuration-block::

.. code-block:: html+jinja

{% form_theme form _self %}

{% block _tasks_widget %}
{% set attr = attr|merge({ 'data-prototype': form_row(prototype) }) %}
<table {{ block('widget_container_attributes') }}>
{% for child in form %}
{{ form_row(child) }}
{% endfor %}
</table>
{% endblock %}

.. code-block:: html+php

<!-- src/AppBundle/Resources/views/Form/_tasks_widget.html.php -->
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I'm stuck how this should look like in the PHP template.


.. _`Owning Side and Inverse Side`: http://docs.doctrine-project.org/en/latest/reference/unitofwork-associations.html
.. _`JSFiddle`: http://jsfiddle.net/847Kf/4/