Skip to content

Commit f8b6dd1

Browse files
committed
some tweaks
1 parent a969b53 commit f8b6dd1

File tree

1 file changed

+45
-49
lines changed

1 file changed

+45
-49
lines changed

cookbook/form/form_collections.rst

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -729,79 +729,75 @@ the relationship between the removed ``Tag`` and ``Task`` object.
729729
updated (whether you're adding new tags or removing existing tags) on
730730
each Tag object itself.
731731

732-
.. _cookbook-form-collections-custom-prototype
732+
.. _cookbook-form-collections-custom-prototype:
733733

734734
Rendering a Custom Prototype
735735
----------------------------
736736

737737
Most of the time the provided prototype will be sufficient for your needs
738-
and does not need to be changed. But if you are in the situation were
739-
you need to have a complete custom prototype, you can render it yourself:
738+
and does not need to be changed. But if you are in the situation were you
739+
need to have a complete custom prototype, you can render it yourself.
740+
741+
The Form component automatically looks for a block whose name follows a certain
742+
schema to decide how to render each entry of the form type collection. For
743+
example, if your form field is named ``tasks``, you will be able to change
744+
the widget for each task as follows:
740745

741746
.. configuration-block::
742747

743748
.. code-block:: html+jinja
744749

745-
<!-- src/Acme/TaskBundle/Resources/views/Task/prototypeTask.html.twig -->
746-
data-prototype="{% filter escape %}
747-
{% include 'AcmeTaskBundle:Task:prototypeTask.html.twig'
748-
with { 'task': form.task.get('prototype') }
749-
%}
750-
{% endfilter %}"
751-
752-
.. code-block:: html+php
753-
754-
<!-- src/Acme/TaskBundle/Resources/views/Task/prototypeTask.html.php -->
755-
data-prototype="<?php
756-
$prototype = $view->render(
757-
'AcmeTaskBundle:Task:prototypeTask.html.php',
758-
array('task' => $form->task->get('prototype'))
759-
);
750+
{% form_theme form _self %}
760751

761-
echo $view->escape($prototype);
762-
?>"
763-
764-
To be not confused let's have a look how the prototype-template might look like.
765-
766-
.. configuration-block::
752+
{% block _tasks_entry_widget %}
753+
<li>
754+
{{ form_widget(task.task) }}
755+
{{ form_widget(task.dueDate) }}
756+
</li>
757+
{% endblock %}
767758

768-
.. code-block:: html+jinja
759+
.. code-block:: html+php
769760

770-
<tr>
771-
<td>{{ form_widget(task.task) }}</td>
772-
<td>{{ form_widget(task.dueDate) }}</td>
773-
</tr>
761+
<!-- src/AppBundle/Resources/views/Form/_tasks_entry_widget.html.php -->
762+
<li>
763+
<?php echo $view['form']->widget($form->task) ?>
764+
<?php echo $view['form']->widget($form->dueDate) ?>
765+
</li>
774766

775-
.. code-block:: html+php
767+
Not only can you override the rendered widget, but you can also change the
768+
complete form row or the label as well. For the ``tasks`` field given above,
769+
the block names would be the following:
776770

777-
<tr>
778-
<td><?php echo $view['form']->widget($task->getTask()) ?></td>
779-
<td><?php echo $view['form']->widget($task->getDueDate()) ?></td>
780-
</tr>
771+
================ =======================
772+
Part of the Form Block Name
773+
================ =======================
774+
``label`` ``_tasks_entry_label``
775+
``widget`` ``_tasks_entry_widget``
776+
``row`` ``_tasks_entry_row``
777+
================ =======================
781778

782-
The included template contains the markup used for the prototype.
783-
This way you can not only easily structure your prototype-markup,
784-
you can also use this markup to render the
785-
contents of the collection when it already holds items:
779+
Then, you only have to ensure to render the collection type's ``data-prototype``
780+
property with the proper prototype so that new entries will be rendered the
781+
same way as existing ones:
786782

787783
.. configuration-block::
788784

789785
.. code-block:: html+jinja
790786

791-
{% for task in tasks %}
792-
{% include 'AcmeTaskBundle:Task:prototypeTask.html.twig'
793-
with { 'form': form.task.vars.form }
794-
%}
795-
{% endfor %}
787+
{% form_theme form _self %}
796788

797-
.. code-block:: html+php
789+
{% block _tasks_widget %}
790+
{% set attr = attr|merge({ 'data-prototype': form_row(prototype) }) %}
791+
<ul {{ block('widget_container_attributes') }}>
792+
{% for child in form %}
793+
{{ form_row(child) }}
794+
{% endfor %}
795+
</ul>
796+
{% endblock %}
798797

799-
<?php foreach ($tasks as $task) ?>
800-
<?php echo $view->render('AcmeTaskBundle:Task:prototypeTask.html.php', array('form' => $form->task->vars->form)); ?>
801-
<?php endforeach; ?>
798+
.. code-block:: html+php
802799

803-
This makes sure the displayed items are the same as the newly inserted
804-
from the prototype.
800+
<!-- src/AppBundle/Resources/views/Form/_tasks_widget.html.php -->
805801

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

0 commit comments

Comments
 (0)