@@ -729,79 +729,75 @@ the relationship between the removed ``Tag`` and ``Task`` object.
729
729
updated (whether you're adding new tags or removing existing tags) on
730
730
each Tag object itself.
731
731
732
- .. _cookbook-form-collections-custom-prototype
732
+ .. _cookbook-form-collections-custom-prototype :
733
733
734
734
Rendering a Custom Prototype
735
735
----------------------------
736
736
737
737
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:
740
745
741
746
.. configuration-block ::
742
747
743
748
.. code-block :: html+jinja
744
749
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 %}
760
751
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 %}
767
758
768
- .. code-block :: html+jinja
759
+ .. code-block :: html+php
769
760
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>
774
766
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:
776
770
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
+ ================ =======================
781
778
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:
786
782
787
783
.. configuration-block ::
788
784
789
785
.. code-block :: html+jinja
790
786
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 %}
796
788
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 %}
798
797
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
802
799
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 -->
805
801
806
802
.. _`Owning Side and Inverse Side` : http://docs.doctrine-project.org/en/latest/reference/unitofwork-associations.html
807
803
.. _`JSFiddle` : http://jsfiddle.net/847Kf/4/
0 commit comments