@@ -143,7 +143,7 @@ helper functions:
143
143
144
144
.. code-block :: html+php
145
145
146
- <?php // src/Acme/StoreBundle/Resources/views/Default/index.html.php ? >
146
+ <!-- src/Acme/StoreBundle/Resources/views/Default/index.html.php -- >
147
147
148
148
<form action="<?php echo $view['router']->generate('store_product') ?>" method="post" <?php echo $view['form']->enctype($form) ?> >
149
149
<?php echo $view['form']->widget($form) ?>
@@ -489,7 +489,7 @@ of code. Of course, you'll usually need much more flexibility when rendering:
489
489
490
490
.. code-block :: html+php
491
491
492
- <?php // src/Acme/StoreBundle/Resources/views/Default/index.html.php ? >
492
+ <!-- // src/Acme/StoreBundle/Resources/views/Default/index.html.php -- >
493
493
494
494
<form action="<?php echo $view['router']->generate('store_product') ?>" method="post" <?php echo $view['form']->enctype($form) ?>>
495
495
<?php echo $view['form']->errors($form) ?>
@@ -901,12 +901,16 @@ how each form "row" renders, change the markup used to render errors, or
901
901
even customize how a textarea tag should be rendered. Nothing is off-limits,
902
902
and different customizations can be used in different places.
903
903
904
- Symfony uses templates to render each and every part of a form. In Twig,
905
- the different pieces of a form - a row, a textarea tag, errors - are represented
906
- by Twig "blocks". To customize any part of how a form renders, you just need
907
- to override the appropriate block.
904
+ Symfony uses templates to render each and every part of a form which are called
905
+ fragments - a row, a textarea tag, errors, etc.
908
906
909
- To understand how this works, let's customize the ``form_row `` output and
907
+ In Twig, the fragments are represented by Twig "blocks". To customize any part
908
+ of how a form renders, you just need to override the appropriate block.
909
+
910
+ In PHP, the fragments are individual template files. To customize any part of how
911
+ a form renders, you just need to create a new template file.
912
+
913
+ To understand how this works, let's customize the ``form_row `` fragment and
910
914
add a class attribute to the ``div `` element that surrounds each row. To
911
915
do this, create a new template file that will store the new markup:
912
916
@@ -929,58 +933,73 @@ do this, create a new template file that will store the new markup:
929
933
.. code-block :: html+php
930
934
931
935
<!-- src/Acme/StoreBundle/Resources/views/Form/field_row.html.php -->
936
+
932
937
<div class="form_row">
933
938
<?php echo $view['form']->label($form, $label) ?>
934
939
<?php echo $view['form']->errors($form) ?>
935
940
<?php echo $view['form']->widget($form, $parameters) ?>
936
941
</div>
937
942
938
- The ``field_row `` block is the name of the block used when rendering most
939
- fields via the ``form_row `` function. To use the ``field_row `` block defined
940
- in this template, add the following to the top of the template that renders
941
- the form:
943
+ The ``field_row `` fragment is used when rendering most fields via the ``form_row ``
944
+ function. To use the ``field_row `` fragment defined in this template, add the
945
+ following to the top of the template that renders the form:
942
946
943
947
.. configuration-block :: php
944
948
945
949
.. code-block :: html+jinja
946
950
947
951
{# src/Acme/StoreBundle/Resources/views/Default/index.html.twig #}
952
+
948
953
{% form_theme form 'AcmeStoreBundle:Form: fields.html.twig' %}
949
954
950
955
<form ...>
951
956
952
- The ``form_theme `` tag "imports" the blocks defined in the template and uses
957
+ .. code-block :: html+php
958
+
959
+ <!-- src/Acme/StoreBundle/Resources/views/Default/index.html.php -->
960
+
961
+ <?php $view['form']->setTheme($form, array('AcmeStoreBundle:Form')) ?>
962
+
963
+ <form ...>
964
+
965
+ The ``form_theme `` tag "imports" the fragments defined in the theme and uses
953
966
them when rendering the form. In other words, when ``form_row `` is called
954
- later in this template, it will use the ``field_row `` block from the
955
- `` fields.html.twig `` template .
967
+ later in this template, it will use the ``field_row `` block from your custom
968
+ theme .
956
969
957
970
To customize any portion of a form, you just need to override the appropriate
958
- block . Knowing exactly which block to override is the subject of the next
971
+ fragment . Knowing exactly which block to override is the subject of the next
959
972
section.
960
973
961
974
In the following section, you'll learn more about how to customize different
962
- portions of a form. For a more extensive discussion, see :doc: `/cookbook/form/twig_form_customization `.
975
+ portions of a form. For a more extensive discussion, see :doc: `/cookbook/form/form_customization `.
963
976
964
977
.. _form-template-blocks :
965
978
966
979
Form Template Blocks
967
980
~~~~~~~~~~~~~~~~~~~~
968
981
969
- Every part of a form that is rendered - HTML form elements, errors, labels, etc -
970
- is defined in a base template as individual Twig blocks. By default, every
971
- block needed is defined in the `form_div_layout.html.twig `_ file that lives inside
972
- the `Twig Bridge `_. Inside this file, you can see every block needed
973
- to render a form and every default field type.
982
+ In Symfony, every fragment a form that is rendered - HTML form elements, errors,
983
+ labels, etc - is defined in a base theme.
984
+
985
+ The fragments are defined as blocks in Twig and as template files in PHP.
974
986
975
- Each block follows the same basic pattern and is broken up into two pieces,
987
+ In Twig, every block needed is defined in the `form_div_layout.html.twig `_ file
988
+ that lives inside the `Twig Bridge `_. Inside this file, you can see every block
989
+ needed to render a form and every default field type.
990
+
991
+ In PHP, the fragments are individual template files. By default they are located in
992
+ the `FrameworkBundle/Resources/views/Form ` folder.
993
+
994
+ Each fragment name follows the same basic pattern and is broken up into two pieces,
976
995
separated by a single underscore character (``_ ``). A few examples are:
977
996
978
997
* ``field_row `` - used by ``form_row `` to render most fields;
979
998
* ``textarea_widget `` - used by ``form_widget `` to render a ``textarea `` field
980
999
type;
981
1000
* ``field_errors `` - used by ``form_errors `` to render errors for a field;
982
1001
983
- Each block follows the same basic pattern: ``type_part ``. The ``type `` portion
1002
+ Each fragment follows the same basic pattern: ``type_part ``. The ``type `` portion
984
1003
corresponds to the field type being rendered (e.g. ``textarea `` or ``checkbox ``)
985
1004
whereas the ``part `` portion corresponds to *what * is being rendered (e.g.
986
1005
``label ``, ``widget ``). By default, there are exactly 7 possible parts of
@@ -1003,37 +1022,34 @@ a form that can be rendered:
1003
1022
+-------------+--------------------------+------------------------------------------------------+
1004
1023
1005
1024
By knowing the field type (e.g. ``textarea ``) and which part you want to
1006
- customize (e.g. ``widget ``), you can construct the block name that needs
1007
- to be overridden (e.g. ``textarea_widget ``). The best way to customize the
1008
- block is to copy it from `form_div_layout.html.twig `_ to a new template, customize
1009
- it, and then use the ``form_theme `` tag as shown in the earlier example.
1025
+ customize (e.g. ``widget ``), you can construct the fragment name that needs
1026
+ to be overridden (e.g. ``textarea_widget ``).
1010
1027
1011
1028
Form Type Block Inheritance
1012
1029
~~~~~~~~~~~~~~~~~~~~~~~~~~~
1013
1030
1014
- In some cases, the block you want to customize will appear to be missing.
1015
- For example, if you look in the `form_div_layout.html.twig `_ file, you'll find
1016
- no ``textarea_errors `` block. So how are the errors for a textarea field
1017
- rendered?
1031
+ In some cases, the fragment you want to customize will appear to be missing.
1032
+ For example, there is no ``textarea_errors `` fragment in the default themes
1033
+ provided with Symfony. So how are the errors for a textarea field rendered?
1018
1034
1019
- The answer is: via the ``field_errors `` block . When Symfony renders the errors
1020
- for a textarea type, it looks first for a ``textarea_errors `` block before
1021
- falling back to the ``field_errors `` block . Each field type has a *parent *
1035
+ The answer is: via the ``field_errors `` fragment . When Symfony renders the errors
1036
+ for a textarea type, it looks first for a ``textarea_errors `` fragment before
1037
+ falling back to the ``field_errors `` fragment . Each field type has a *parent *
1022
1038
type (the parent type of ``textarea `` is ``field ``), and Symfony uses the
1023
- block for the parent type if the base block doesn't exist.
1039
+ fragment for the parent type if the base fragment doesn't exist.
1024
1040
1025
1041
So, to override the errors for *only * ``textarea `` fields, copy the
1026
- ``field_errors `` block , rename it to ``textarea_errors `` and customize it. To
1042
+ ``field_errors `` fragment , rename it to ``textarea_errors `` and customize it. To
1027
1043
override the default error rendering for *all * fields, copy and customize the
1028
- ``field_errors `` block directly.
1044
+ ``field_errors `` fragment directly.
1029
1045
1030
1046
Global Form Theming
1031
1047
~~~~~~~~~~~~~~~~~~~
1032
1048
1033
- So far , you've seen how you can use the ``form_theme `` Twig block in a template
1034
- to import form customizations that will be used inside that template. You can
1035
- also tell Symfony to automatically use certain form customizations for all
1036
- templates in your application. To automatically include the customized blocks
1049
+ When using Twig , you've seen how you can use the ``form_theme `` Twig tag in a
1050
+ template to import form customizations that will be used inside that template.
1051
+ You can also tell Symfony to automatically use certain form customizations for
1052
+ all templates in your application. To automatically include the customized blocks
1037
1053
from the ``fields.html.twig `` template created earlier, modify your application
1038
1054
configuration file:
1039
1055
@@ -1042,6 +1058,7 @@ configuration file:
1042
1058
.. code-block :: yaml
1043
1059
1044
1060
# app/config/config.yml
1061
+
1045
1062
twig :
1046
1063
form :
1047
1064
resources :
@@ -1052,6 +1069,7 @@ configuration file:
1052
1069
.. code-block :: xml
1053
1070
1054
1071
<!-- app/config/config.xml -->
1072
+
1055
1073
<twig : config ...>
1056
1074
<twig : form >
1057
1075
<resource >form_div_layout.html.twig</resource >
@@ -1063,6 +1081,7 @@ configuration file:
1063
1081
.. code-block :: php
1064
1082
1065
1083
// app/config/config.php
1084
+
1066
1085
$container->loadFromExtension('twig', array(
1067
1086
'form' => array('resources' => array(
1068
1087
'form_div_layout.html.twig',
@@ -1074,7 +1093,13 @@ configuration file:
1074
1093
Any blocks inside the ``fields.html.twig `` template are now used globally
1075
1094
to define form output.
1076
1095
1077
- .. sidebar :: Customizing Form Output all in a Single File
1096
+ .. note ::
1097
+
1098
+ The `form_div_layout.html.twig `_ theme is automatically added as a default
1099
+ when it is not provided in the list of resources. It would not have been
1100
+ necessary to provide it in the examples above.
1101
+
1102
+ .. sidebar :: Customizing Form Output all in a Single File in Twig
1078
1103
1079
1104
You can also customize a form block right inside the template where that
1080
1105
customization is needed :
@@ -1109,10 +1134,68 @@ to define form output.
1109
1134
1110
1135
{% block text_widget %}
1111
1136
<div class="text_widget">
1112
- <input type="text" {{ block('attributes ') }} value="{{ value }}" />
1137
+ <input type="text" {{ block('widget_attributes ') }} value="{{ value }}" />
1113
1138
</div>
1114
1139
{% endblock %}
1115
1140
1141
+ When using PHP, you've seen how you can use the ``setTheme `` helper method in a
1142
+ template to import form customizations that will be used inside that template.
1143
+ You can also tell Symfony to automatically use certain form customizations for all
1144
+ templates in your application. To automatically include the customized templates
1145
+ from the `Acme/StoreBundle/Resources/views/Form ` folder created earlier, modify
1146
+ your application configuration file:
1147
+
1148
+ .. configuration-block ::
1149
+
1150
+ .. code-block :: yaml
1151
+
1152
+ # app/config/config.yml
1153
+
1154
+ framework :
1155
+ templating :
1156
+ form :
1157
+ resources :
1158
+ - ' FrameworkBundle:Form'
1159
+ - ' AcmeStoreBundle:Form'
1160
+ # ...
1161
+
1162
+
1163
+ .. code-block :: xml
1164
+
1165
+ <!-- app/config/config.xml -->
1166
+
1167
+ <framework : config ...>
1168
+ <framework : templating >
1169
+ <framework : form >
1170
+ <resource >FrameworkBundle:Form</resource >
1171
+ <resource >AcmeStoreBundle:Form</resource >
1172
+ </framework : form >
1173
+ </framework : templating >
1174
+ <!-- ... -->
1175
+ </framework : config >
1176
+
1177
+ .. code-block :: php
1178
+
1179
+ // app/config/config.php
1180
+
1181
+ $container->loadFromExtension('framework', array(
1182
+ 'templating' => array('form' =>
1183
+ array('resources' => array(
1184
+ 'FrameworkBundle:Form',
1185
+ 'AcmeStoreBundle:Form',
1186
+ )))
1187
+ // ...
1188
+ ));
1189
+
1190
+ Any framents inside the `Acme/StoreBundle/Resources/views/Form ` folder are now
1191
+ used globally to define form output.
1192
+
1193
+ .. note ::
1194
+
1195
+ The `FrameworkBundle:Form `_ theme is automatically added as a default when
1196
+ it is not provided in the list of resources. It would not have been necessary
1197
+ to provide it in the examples above.
1198
+
1116
1199
.. index ::
1117
1200
single: Forms; CSRF Protection
1118
1201
@@ -1189,9 +1272,10 @@ Learn more from the Cookbook
1189
1272
* :doc: `/cookbook/doctrine/file_uploads `
1190
1273
* :doc: `File Field Reference </reference/forms/types/file >`
1191
1274
* :doc: `Creating Custom Field Types </cookbook/form/create_custom_field_type >`
1192
- * :doc: `/cookbook/form/twig_form_customization `
1275
+ * :doc: `/cookbook/form/form_customization `
1193
1276
1194
1277
.. _`Symfony2 Form Component` : https://github.com/symfony/Form
1195
1278
.. _`Twig Bridge` : https://github.com/symfony/symfony/tree/master/src/Symfony/Bridge/Twig
1196
1279
.. _`form_div_layout.html.twig` : https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
1280
+ .. _`FrameworkBundle:Form` : https://github.com/symfony/symfony/tree/master/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form
1197
1281
.. _`Cross-site request forgery` : http://en.wikipedia.org/wiki/Cross-site_request_forgery
0 commit comments