@@ -23,6 +23,9 @@ label you want to display as the second argument.
23
23
{{ form_label(form.name, 'Your Name', {'label_attr': {'class': 'foo'}}) }}
24
24
{{ form_label(form.name, null, {'label': 'Your name', 'label_attr': {'class': 'foo'}}) }}
25
25
26
+ See ":ref: `twig-reference-form-variables `" to learn about the ``variables ``
27
+ argument.
28
+
26
29
form_errors(form.name)
27
30
----------------------
28
31
@@ -50,6 +53,11 @@ The second argument to ``form_widget`` is an array of variables. The most
50
53
common variable is ``attr ``, which is an array of HTML attributes to apply
51
54
to the HTML widget. In some cases, certain types also have other template-related
52
55
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) ``).
58
+
59
+ See ":ref: `twig-reference-form-variables `" to learn more about the ``variables ``
60
+ argument.
53
61
54
62
form_row(form.name, variables)
55
63
------------------------------
@@ -66,6 +74,9 @@ The second argument to ``form_row`` is an array of variables. The templates
66
74
provided in Symfony only allow to override the label as shown in the example
67
75
above.
68
76
77
+ See ":ref: `twig-reference-form-variables `" to learn about the ``variables ``
78
+ argument.
79
+
69
80
form_rest(form, variables)
70
81
--------------------------
71
82
@@ -87,4 +98,68 @@ good idea to include this in your form tag:
87
98
88
99
.. code-block :: html+jinja
89
100
90
- <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