@@ -901,14 +901,15 @@ 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 which are called
905
- fragments - a row, a textarea tag, errors, etc .
904
+ Symfony uses templates to render each and every part of a form, such as
905
+ field `` label `` tags, `` input `` tags, error messages and everything else .
906
906
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.
907
+ In Twig, each form "fragment" is represented by a Twig block . To customize
908
+ any part of how a form renders, you just need to override the appropriate block.
909
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.
910
+ In PHP, each form "fragment" is rendered via an individual template file.
911
+ To customize any part of how a form renders, you just need to override the
912
+ existing template by creating a new one.
912
913
913
914
To understand how this works, let's customize the ``form_row `` fragment and
914
915
add a class attribute to the ``div `` element that surrounds each row. To
@@ -940,9 +941,10 @@ do this, create a new template file that will store the new markup:
940
941
<?php echo $view['form']->widget($form, $parameters) ?>
941
942
</div>
942
943
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:
944
+ The ``field_row `` form fragment is used when rendering most fields via the
945
+ ``form_row `` function. To tell the component to use your new ``field_row ``
946
+ fragment defined above, add the following to the top of the template that
947
+ renders the form:
946
948
947
949
.. configuration-block :: php
948
950
@@ -962,14 +964,14 @@ following to the top of the template that renders the form:
962
964
963
965
<form ...>
964
966
965
- The ``form_theme `` tag "imports" the fragments defined in the theme and uses
966
- them when rendering the form. In other words, when ``form_row `` is called
967
- later in this template, it will use the ``field_row `` block from your custom
968
- theme.
967
+ The ``form_theme `` tag (in Twig) "imports" the fragments defined in the given
968
+ template and uses them when rendering the form. In other words, when ``form_row ``
969
+ is called later in this template, it will use the ``field_row `` block from
970
+ your custom theme.
969
971
970
972
To customize any portion of a form, you just need to override the appropriate
971
- fragment. Knowing exactly which block to override is the subject of the next
972
- section.
973
+ fragment. Knowing exactly which block or file to override is the subject of
974
+ the next section.
973
975
974
976
In the following section, you'll learn more about how to customize different
975
977
portions of a form. For a more extensive discussion, see :doc: `/cookbook/form/form_customization `.
@@ -979,17 +981,16 @@ portions of a form. For a more extensive discussion, see :doc:`/cookbook/form/fo
979
981
Form Template Blocks
980
982
~~~~~~~~~~~~~~~~~~~~
981
983
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.
984
+ In Symfony, every part a form that is rendered - HTML form elements, errors,
985
+ labels, etc - is defined in a base theme, which is a collection of blocks
986
+ in Twig and a collection of template files in PHP.
986
987
987
988
In Twig, every block needed is defined in the `form_div_layout.html.twig `_ file
988
989
that lives inside the `Twig Bridge `_. Inside this file, you can see every block
989
990
needed to render a form and every default field type.
990
991
991
992
In PHP, the fragments are individual template files. By default they are located in
992
- the `FrameworkBundle/ Resources/views/Form ` folder .
993
+ the `Resources/views/Form ` directory of the framework bundle (` view on GitHub `_) .
993
994
994
995
Each fragment name follows the same basic pattern and is broken up into two pieces,
995
996
separated by a single underscore character (``_ ``). A few examples are:
@@ -1046,6 +1047,12 @@ override the default error rendering for *all* fields, copy and customize the
1046
1047
Global Form Theming
1047
1048
~~~~~~~~~~~~~~~~~~~
1048
1049
1050
+ In addition to *theming * individual templates, you can also tell Symfony
1051
+ to import form customizations across your entire project.
1052
+
1053
+ Twig
1054
+ ....
1055
+
1049
1056
When using Twig, you've seen how you can use the ``form_theme `` Twig tag in a
1050
1057
template to import form customizations that will be used inside that template.
1051
1058
You can also tell Symfony to automatically use certain form customizations for
@@ -1062,7 +1069,6 @@ configuration file:
1062
1069
twig :
1063
1070
form :
1064
1071
resources :
1065
- - ' form_div_layout.html.twig'
1066
1072
- ' AcmeStoreBundle:Form:fields.html.twig'
1067
1073
# ...
1068
1074
@@ -1072,7 +1078,6 @@ configuration file:
1072
1078
1073
1079
<twig : config ...>
1074
1080
<twig : form >
1075
- <resource >form_div_layout.html.twig</resource >
1076
1081
<resource >AcmeStoreBundle:Form:fields.html.twig</resource >
1077
1082
</twig : form >
1078
1083
<!-- ... -->
@@ -1084,7 +1089,6 @@ configuration file:
1084
1089
1085
1090
$container->loadFromExtension('twig', array(
1086
1091
'form' => array('resources' => array(
1087
- 'form_div_layout.html.twig',
1088
1092
'AcmeStoreBundle:Form:fields.html.twig',
1089
1093
))
1090
1094
// ...
@@ -1093,12 +1097,6 @@ configuration file:
1093
1097
Any blocks inside the ``fields.html.twig `` template are now used globally
1094
1098
to define form output.
1095
1099
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
1100
.. sidebar :: Customizing Form Output all in a Single File in Twig
1103
1101
1104
1102
You can also customize a form block right inside the template where that
@@ -1138,11 +1136,14 @@ to define form output.
1138
1136
</div>
1139
1137
{% endblock %}
1140
1138
1139
+ PHP
1140
+ ...
1141
+
1141
1142
When using PHP, you've seen how you can use the ``setTheme `` helper method in a
1142
1143
template to import form customizations that will be used inside that template.
1143
1144
You can also tell Symfony to automatically use certain form customizations for all
1144
1145
templates in your application. To automatically include the customized templates
1145
- from the `Acme/StoreBundle/Resources/views/Form ` folder created earlier, modify
1146
+ from the `Acme/StoreBundle/Resources/views/Form ` directroy created earlier, modify
1146
1147
your application configuration file:
1147
1148
1148
1149
.. configuration-block ::
@@ -1155,7 +1156,6 @@ your application configuration file:
1155
1156
templating :
1156
1157
form :
1157
1158
resources :
1158
- - ' FrameworkBundle:Form'
1159
1159
- ' AcmeStoreBundle:Form'
1160
1160
# ...
1161
1161
@@ -1167,7 +1167,6 @@ your application configuration file:
1167
1167
<framework : config ...>
1168
1168
<framework : templating >
1169
1169
<framework : form >
1170
- <resource >FrameworkBundle:Form</resource >
1171
1170
<resource >AcmeStoreBundle:Form</resource >
1172
1171
</framework : form >
1173
1172
</framework : templating >
@@ -1181,21 +1180,14 @@ your application configuration file:
1181
1180
$container->loadFromExtension('framework', array(
1182
1181
'templating' => array('form' =>
1183
1182
array('resources' => array(
1184
- 'FrameworkBundle:Form',
1185
1183
'AcmeStoreBundle:Form',
1186
1184
)))
1187
1185
// ...
1188
1186
));
1189
1187
1190
- Any framents inside the `Acme/StoreBundle/Resources/views/Form ` folder are now
1188
+ Any framgents inside the `Acme/StoreBundle/Resources/views/Form ` folder are now
1191
1189
used globally to define form output.
1192
1190
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
-
1199
1191
.. index ::
1200
1192
single: Forms; CSRF Protection
1201
1193
@@ -1277,5 +1269,5 @@ Learn more from the Cookbook
1277
1269
.. _`Symfony2 Form Component` : https://github.com/symfony/Form
1278
1270
.. _`Twig Bridge` : https://github.com/symfony/symfony/tree/master/src/Symfony/Bridge/Twig
1279
1271
.. _`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
1281
1272
.. _`Cross-site request forgery` : http://en.wikipedia.org/wiki/Cross-site_request_forgery
1273
+ .. _`view on GitHub` : https://github.com/symfony/symfony/tree/master/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form
0 commit comments