Skip to content

Commit 6761de4

Browse files
committed
Merge branch 'form/php' of git://github.com/vicb/symfony-docs into vicb_formphp
Conflicts: cookbook/form/twig_form_customization.rst cookbook/index.rst cookbook/map.rst.inc
2 parents 4ae5360 + 0b76989 commit 6761de4

File tree

7 files changed

+1033
-634
lines changed

7 files changed

+1033
-634
lines changed

book/forms.rst

Lines changed: 128 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ helper functions:
143143

144144
.. code-block:: html+php
145145

146-
<?php // src/Acme/StoreBundle/Resources/views/Default/index.html.php ?>
146+
<!-- src/Acme/StoreBundle/Resources/views/Default/index.html.php -->
147147

148148
<form action="<?php echo $view['router']->generate('store_product') ?>" method="post" <?php echo $view['form']->enctype($form) ?> >
149149
<?php echo $view['form']->widget($form) ?>
@@ -489,7 +489,7 @@ of code. Of course, you'll usually need much more flexibility when rendering:
489489

490490
.. code-block:: html+php
491491

492-
<?php // src/Acme/StoreBundle/Resources/views/Default/index.html.php ?>
492+
<!-- // src/Acme/StoreBundle/Resources/views/Default/index.html.php -->
493493

494494
<form action="<?php echo $view['router']->generate('store_product') ?>" method="post" <?php echo $view['form']->enctype($form) ?>>
495495
<?php echo $view['form']->errors($form) ?>
@@ -901,12 +901,16 @@ how each form "row" renders, change the markup used to render errors, or
901901
even customize how a textarea tag should be rendered. Nothing is off-limits,
902902
and different customizations can be used in different places.
903903

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.
908906

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
910914
add a class attribute to the ``div`` element that surrounds each row. To
911915
do this, create a new template file that will store the new markup:
912916

@@ -929,58 +933,73 @@ do this, create a new template file that will store the new markup:
929933
.. code-block:: html+php
930934

931935
<!-- src/Acme/StoreBundle/Resources/views/Form/field_row.html.php -->
936+
932937
<div class="form_row">
933938
<?php echo $view['form']->label($form, $label) ?>
934939
<?php echo $view['form']->errors($form) ?>
935940
<?php echo $view['form']->widget($form, $parameters) ?>
936941
</div>
937942

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:
942946

943947
.. configuration-block:: php
944948

945949
.. code-block:: html+jinja
946950

947951
{# src/Acme/StoreBundle/Resources/views/Default/index.html.twig #}
952+
948953
{% form_theme form 'AcmeStoreBundle:Form:fields.html.twig' %}
949954

950955
<form ...>
951956

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
953966
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.
956969

957970
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
959972
section.
960973

961974
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`.
963976

964977
.. _form-template-blocks:
965978

966979
Form Template Blocks
967980
~~~~~~~~~~~~~~~~~~~~
968981

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.
974986

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,
976995
separated by a single underscore character (``_``). A few examples are:
977996

978997
* ``field_row`` - used by ``form_row`` to render most fields;
979998
* ``textarea_widget`` - used by ``form_widget`` to render a ``textarea`` field
980999
type;
9811000
* ``field_errors`` - used by ``form_errors`` to render errors for a field;
9821001

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
9841003
corresponds to the field type being rendered (e.g. ``textarea`` or ``checkbox``)
9851004
whereas the ``part`` portion corresponds to *what* is being rendered (e.g.
9861005
``label``, ``widget``). By default, there are exactly 7 possible parts of
@@ -1003,37 +1022,34 @@ a form that can be rendered:
10031022
+-------------+--------------------------+------------------------------------------------------+
10041023

10051024
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``).
10101027

10111028
Form Type Block Inheritance
10121029
~~~~~~~~~~~~~~~~~~~~~~~~~~~
10131030

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?
10181034

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*
10221038
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.
10241040

10251041
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
10271043
override the default error rendering for *all* fields, copy and customize the
1028-
``field_errors`` block directly.
1044+
``field_errors`` fragment directly.
10291045

10301046
Global Form Theming
10311047
~~~~~~~~~~~~~~~~~~~
10321048

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
10371053
from the ``fields.html.twig`` template created earlier, modify your application
10381054
configuration file:
10391055

@@ -1042,6 +1058,7 @@ configuration file:
10421058
.. code-block:: yaml
10431059
10441060
# app/config/config.yml
1061+
10451062
twig:
10461063
form:
10471064
resources:
@@ -1052,6 +1069,7 @@ configuration file:
10521069
.. code-block:: xml
10531070
10541071
<!-- app/config/config.xml -->
1072+
10551073
<twig:config ...>
10561074
<twig:form>
10571075
<resource>form_div_layout.html.twig</resource>
@@ -1063,6 +1081,7 @@ configuration file:
10631081
.. code-block:: php
10641082
10651083
// app/config/config.php
1084+
10661085
$container->loadFromExtension('twig', array(
10671086
'form' => array('resources' => array(
10681087
'form_div_layout.html.twig',
@@ -1074,7 +1093,13 @@ configuration file:
10741093
Any blocks inside the ``fields.html.twig`` template are now used globally
10751094
to define form output.
10761095

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
10781103

10791104
You can also customize a form block right inside the template where that
10801105
customization is needed :
@@ -1109,10 +1134,68 @@ to define form output.
11091134

11101135
{% block text_widget %}
11111136
<div class="text_widget">
1112-
<input type="text" {{ block('attributes') }} value="{{ value }}" />
1137+
<input type="text" {{ block('widget_attributes') }} value="{{ value }}" />
11131138
</div>
11141139
{% endblock %}
11151140

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+
11161199
.. index::
11171200
single: Forms; CSRF Protection
11181201

@@ -1189,9 +1272,10 @@ Learn more from the Cookbook
11891272
* :doc:`/cookbook/doctrine/file_uploads`
11901273
* :doc:`File Field Reference </reference/forms/types/file>`
11911274
* :doc:`Creating Custom Field Types </cookbook/form/create_custom_field_type>`
1192-
* :doc:`/cookbook/form/twig_form_customization`
1275+
* :doc:`/cookbook/form/form_customization`
11931276

11941277
.. _`Symfony2 Form Component`: https://github.com/symfony/Form
11951278
.. _`Twig Bridge`: https://github.com/symfony/symfony/tree/master/src/Symfony/Bridge/Twig
11961279
.. _`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
11971281
.. _`Cross-site request forgery`: http://en.wikipedia.org/wiki/Cross-site_request_forgery

0 commit comments

Comments
 (0)