Skip to content

Commit de915d3

Browse files
committed
[forms] Adding sections talking about form variables
Also changing the form block template to be specific to 2.0 - which will be a pain to keep up, but it's quite a bit different between the versions
1 parent 17b01d0 commit de915d3

File tree

3 files changed

+101
-7
lines changed

3 files changed

+101
-7
lines changed

book/forms.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,6 @@ Learn more from the Cookbook
15281528
.. _`Symfony2 Form Component`: https://github.com/symfony/Form
15291529
.. _`DateTime`: http://php.net/manual/en/class.datetime.php
15301530
.. _`Twig Bridge`: https://github.com/symfony/symfony/tree/master/src/Symfony/Bridge/Twig
1531-
.. _`form_div_layout.html.twig`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
1531+
.. _`form_div_layout.html.twig`: https://github.com/symfony/symfony/blob/2.0/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
15321532
.. _`Cross-site request forgery`: http://en.wikipedia.org/wiki/Cross-site_request_forgery
15331533
.. _`view on GitHub`: https://github.com/symfony/symfony/tree/master/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form

cookbook/form/form_customization.rst

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,4 +907,28 @@ To render a help message below a field, pass in a ``help`` variable:
907907
.. tip::
908908
See :ref:`cookbook-form-theming-methods` for how to apply this customization.
909909

910-
.. _`form_div_layout.html.twig`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
910+
Using Form Variables
911+
--------------------
912+
913+
Most of the functions available for rendering different parts of a form (e.g.
914+
the form widget, form label, form widget, etc) also allow you to make certain
915+
customizations directly. Look at the following example:
916+
917+
.. configuration-block::
918+
919+
.. code-block:: jinja
920+
921+
{# render a widget, but add a "foo" class to it #}
922+
{{ form_widget(form.name, { 'attr': {'class': 'foo'} }) }}
923+
924+
.. code-block:: php
925+
926+
<!-- render a widget, but add a "foo" class to it -->
927+
<?php echo $view['form']->widget($form['name'], array('attr' => array(
928+
'class' => 'foo',
929+
))) ?>
930+
931+
The array passed as the second argument contains form "variables". For
932+
more details about this concept in Twig, see :ref:`twig-reference-form-variables`.
933+
934+
.. _`form_div_layout.html.twig`: https://github.com/symfony/symfony/blob/2.0/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

reference/forms/twig_reference.rst

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ label you want to display as the second argument.
2323
{{ form_label(form.name, 'Your Name', { 'attr': {'class': 'foo'} }) }}
2424
{{ form_label(form.name, null, { 'label': 'Your name', 'attr': {'class': 'foo'} }) }}
2525
26+
See ":ref:`twig-reference-form-variables`" to learn about the ``variables``
27+
argument.
28+
2629
form_errors(form.name)
2730
----------------------
2831

@@ -50,11 +53,11 @@ The second argument to ``form_widget`` is an array of variables. The most
5053
common variable is ``attr``, which is an array of HTML attributes to apply
5154
to the HTML widget. In some cases, certain types also have other template-related
5255
options that can be passed. These are discussed on a type-by-type basis.
56+
The ``attributes`` are not applied recursively to child fields if you're
57+
rendering many fields at once (e.g. ``form_widget(form)``).
5358

54-
.. note::
55-
56-
In case the first argument is a form, all ``variables`` will
57-
only be applied to the form itself and not its children.
59+
See ":ref:`twig-reference-form-variables`" to learn more about the ``variables``
60+
argument.
5861

5962
form_row(form.name, variables)
6063
------------------------------
@@ -71,6 +74,9 @@ The second argument to ``form_row`` is an array of variables. The templates
7174
provided in Symfony only allow to override the label as shown in the example
7275
above.
7376

77+
See ":ref:`twig-reference-form-variables`" to learn about the ``variables``
78+
argument.
79+
7480
form_rest(form, variables)
7581
--------------------------
7682

@@ -92,4 +98,68 @@ good idea to include this in your form tag:
9298

9399
.. code-block:: html+jinja
94100

95-
<form action="{{ path('form_submit') }}" method="post" {{ form_enctype(form) }}>
101+
<form action="{{ path('form_submit') }}" method="post" {{ form_enctype(form) }}>
102+
103+
.. _`twig-reference-form-variables`:
104+
105+
More about Form "Variables"
106+
---------------------------
107+
108+
In almost every Twig function above, the final argument is an array of "variables"
109+
that are used when rendering that one part of the form. For example, the
110+
following would render the "widget" for a field, and modify its attributes
111+
to include a special class:
112+
113+
.. code-block:: jinja
114+
115+
{# render a widget, but add a "foo" class to it #}
116+
{{ form_widget(form.name, { 'attr': {'class': 'foo'} }) }}
117+
118+
The purpose of these variables - what they do & where they come from - may
119+
not be immediately clear, but they're incredibly powerful. Whenever you
120+
render any part of a form, the block that renders it makes use of a number
121+
of variables. By default, these blocks live inside `form_div_layout.html.twig`_.
122+
123+
Look at the ``generic_label`` as an example:
124+
125+
.. code-block:: jinja
126+
127+
{% block generic_label %}
128+
{% if required %}
129+
{% set attr = attr|merge({'class': attr.class|default('') ~ ' required'}) %}
130+
{% endif %}
131+
<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>{{ label|trans }}</label>
132+
{% endblock %}
133+
134+
This block makes use of 3 variables: ``required``, ``attr`` and ``label``.
135+
These variables are made available by the form rendering system. But more
136+
importantly, these are the variables that you can override when calling ``form_label``
137+
(since in this example, you're rendering the label).
138+
139+
The exact variables available to override depends on which part of the form
140+
you're rendering (e.g. label versus widget) and which field you're rendering
141+
(e.g. a ``choice`` widget has an extra ``expanded`` option). If you get comfortable
142+
with looking through `form_div_layout.html.twig`_, you'll always be able
143+
to see what options you have available.
144+
145+
.. tip::
146+
147+
Behind the scenes, these variables are made available to the ``FormView``
148+
object of your form when the form component calls ``buildView`` and ``buildViewBottomUp``
149+
on each "node" of your form tree. To see what "view" variables a particularly
150+
field has, find the source code for the form field (and its parent fields)
151+
and look at the above two functions.
152+
153+
.. note::
154+
155+
If you're rendering an entire form at once (or an entire embedded form),
156+
the ``variables`` argument will only be applied to the form itself and
157+
not its children. In other words, the following will **not** pass a "foo"
158+
class attribute to all of the child fields in the form:
159+
160+
.. code-block:: jinja
161+
162+
{# does **not** work - the variables are not recursive #}
163+
{{ form_widget(form, { 'attr': {'class': 'foo'} }) }}
164+
165+
.. _`form_div_layout.html.twig`: https://github.com/symfony/symfony/blob/2.0/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

0 commit comments

Comments
 (0)