4
4
Twig Template Form Function and Variable Reference
5
5
==================================================
6
6
7
- When working with forms in a template, there are two powerful things at your
8
- disposal:
7
+ When working with forms in a template, there are two powerful things at
8
+ your disposal:
9
9
10
- * :ref: `Functions <reference-form-twig-functions >` for rendering each part of a form
11
- * :ref: `Variables <twig-reference-form-variables >` for getting *any * information about any field
10
+ * :ref: `Functions <reference-form-twig-functions >` for rendering each part
11
+ of a form;
12
+ * :ref: `Variables <twig-reference-form-variables >` for getting *any * information
13
+ about any field.
12
14
13
15
You'll use functions often to render your fields. Variables, on the other
14
16
hand, are less commonly-used, but infinitely powerful since you can access
15
- a fields label, id attribute, errors, and anything else about the field.
17
+ a fields label, id attribute, errors and anything else about the field.
16
18
17
19
.. _reference-form-twig-functions :
18
20
19
21
Form Rendering Functions
20
22
------------------------
21
23
22
24
This reference manual covers all the possible Twig functions available for
23
- rendering forms. There are several different functions available, and each
24
- is responsible for rendering a different part of a form (e.g. labels, errors ,
25
- widgets, etc).
25
+ rendering forms. There are several different functions available and
26
+ each is responsible for rendering a different part of a form (e.g. labels,
27
+ errors, widgets, etc).
26
28
27
29
.. _reference-forms-twig-form :
28
30
@@ -76,8 +78,8 @@ Renders the end tag of a form.
76
78
77
79
{{ form_end(form) }}
78
80
79
- This helper also outputs ``form_rest() `` unless you set ``render_rest `` to
80
- false:
81
+ This helper also outputs ``form_rest() `` unless you set ``render_rest ``
82
+ to false:
81
83
82
84
.. code-block :: jinja
83
85
@@ -98,7 +100,11 @@ label you want to display as the second argument.
98
100
99
101
{# The two following syntaxes are equivalent #}
100
102
{{ form_label(form.name, 'Your Name', {'label_attr': {'class': 'foo'}}) }}
101
- {{ form_label(form.name, null, {'label': 'Your name', 'label_attr': {'class': 'foo'}}) }}
103
+
104
+ {{ form_label(form.name, null, {
105
+ 'label': 'Your name',
106
+ 'label_attr': {'class': 'foo'}
107
+ }) }}
102
108
103
109
See ":ref: `twig-reference-form-variables `" to learn about the ``variables ``
104
110
argument.
@@ -122,8 +128,8 @@ Renders any errors for the given field.
122
128
form_widget(view, variables)
123
129
----------------------------
124
130
125
- Renders the HTML widget of a given field. If you apply this to an entire form
126
- or collection of fields, each underlying form row will be rendered.
131
+ Renders the HTML widget of a given field. If you apply this to an entire
132
+ form or collection of fields, each underlying form row will be rendered.
127
133
128
134
.. code-block :: jinja
129
135
@@ -181,8 +187,8 @@ form_enctype(view)
181
187
182
188
.. note ::
183
189
184
- This helper was deprecated in Symfony 2.3 and will be removed in Symfony 3.0.
185
- You should use ``form_start() `` instead.
190
+ This helper was deprecated in Symfony 2.3 and will be removed in Symfony
191
+ 3.0. You should use ``form_start() `` instead.
186
192
187
193
If the form contains at least one file upload field, this will render the
188
194
required ``enctype="multipart/form-data" `` form attribute. It's always a
@@ -204,7 +210,8 @@ selectedchoice(selected_value)
204
210
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
205
211
206
212
This test will check if the current choice is equal to the ``selected_value ``
207
- or if the current choice is in the array (when ``selected_value `` is an array).
213
+ or if the current choice is in the array (when ``selected_value `` is an
214
+ array).
208
215
209
216
.. code-block :: jinja
210
217
@@ -221,7 +228,7 @@ More about Form Variables
221
228
222
229
In almost every Twig function above, the final argument is an array of "variables"
223
230
that are used when rendering that one part of the form. For example, the
224
- following would render the "widget" for a field, and modify its attributes
231
+ following would render the "widget" for a field and modify its attributes
225
232
to include a special class:
226
233
227
234
.. code-block :: jinja
@@ -242,41 +249,52 @@ Look at the ``form_label`` as an example:
242
249
{% if not compound %}
243
250
{% set label_attr = label_attr|merge({'for': id}) %}
244
251
{% endif %}
252
+
245
253
{% if required %}
246
- {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
254
+ {% set label_attr = label_attr|merge({
255
+ 'class': (label_attr.class|default('') ~ ' required')|trim
256
+ }) %}
247
257
{% endif %}
258
+
248
259
{% if label is empty %}
249
260
{% set label = name|humanize %}
250
261
{% endif %}
251
- <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
262
+
263
+ <label
264
+ {% for attrname, attrvalue in label_attr -%}
265
+ {{ attrname }}="{{ attrvalue }}"
266
+ {%- endfor %}
267
+ >
268
+ {{ label|trans({}, translation_domain) }}
269
+ </label>
252
270
{% endblock form_label %}
253
271
254
- This block makes use of several variables: ``compound ``, ``label_attr ``, `` required ``,
255
- ``label ``, ``name `` and ``translation_domain ``.
256
- These variables are made available by the form rendering system. But more
257
- importantly, these are the variables that you can override when calling ``form_label ``
258
- (since in this example, you're rendering the label).
272
+ This block makes use of several variables: ``compound ``, ``label_attr ``,
273
+ ``required ``, `` label ``, ``name `` and ``translation_domain ``. These variables
274
+ are made available by the form rendering system. But more importantly, these
275
+ are the variables that you can override when calling ``form_label `` (since
276
+ in this example, you're rendering the label).
259
277
260
278
The exact variables available to override depends on which part of the form
261
279
you're rendering (e.g. label versus widget) and which field you're rendering
262
- (e.g. a ``choice `` widget has an extra ``expanded `` option). If you get comfortable
263
- with looking through `form_div_layout.html.twig `_, you'll always be able
264
- to see what options you have available.
280
+ (e.g. a ``choice `` widget has an extra ``expanded `` option). If you get
281
+ comfortable with looking through `form_div_layout.html.twig `_, you'll always
282
+ be able to see what options you have available.
265
283
266
284
.. tip ::
267
285
268
286
Behind the scenes, these variables are made available to the ``FormView ``
269
- object of your form when the Form component calls ``buildView `` and `` finishView ``
270
- on each "node" of your form tree. To see what "view" variables a particular
271
- field has, find the source code for the form field (and its parent fields)
272
- and look at the above two functions.
287
+ object of your form when the Form component calls ``buildView `` and
288
+ `` finishView `` on each "node" of your form tree. To see what "view"
289
+ variables a particular field has, find the source code for the form
290
+ field (and its parent fields) and look at the above two functions.
273
291
274
292
.. note ::
275
293
276
294
If you're rendering an entire form at once (or an entire embedded form),
277
295
the ``variables `` argument will only be applied to the form itself and
278
- not its children. In other words, the following will **not ** pass a "foo"
279
- class attribute to all of the child fields in the form:
296
+ not its children. In other words, the following will **not ** pass a
297
+ "foo" class attribute to all of the child fields in the form:
280
298
281
299
.. code-block :: jinja
282
300
@@ -292,10 +310,10 @@ The following variables are common to every field type. Certain field types
292
310
may have even more variables and some variables here only really apply to
293
311
certain types.
294
312
295
- Assuming you have a ``form `` variable in your template, and you want to reference
296
- the variables on the ``name `` field, accessing the variables is done by using
297
- a public ``vars `` property on the :class: ` Symfony \\ Component \\ Form \\ FormView `
298
- object:
313
+ Assuming you have a ``form `` variable in your template and you want to
314
+ reference the variables on the ``name `` field, accessing the variables is
315
+ done by using a public ``vars `` property on the
316
+ :class: ` Symfony \\ Component \\ Form \\ FormView ` object:
299
317
300
318
.. configuration-block ::
301
319
0 commit comments