44Twig Template Form Function and Variable Reference
55==================================================
66
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:
99
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.
1214
1315You'll use functions often to render your fields. Variables, on the other
1416hand, 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.
1618
1719.. _reference-form-twig-functions :
1820
1921Form Rendering Functions
2022------------------------
2123
2224This 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).
2628
2729.. _reference-forms-twig-form :
2830
@@ -76,8 +78,8 @@ Renders the end tag of a form.
7678
7779 {{ form_end(form) }}
7880
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:
8183
8284.. code-block :: jinja
8385
@@ -98,7 +100,11 @@ label you want to display as the second argument.
98100
99101 {# The two following syntaxes are equivalent #}
100102 {{ 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+ }) }}
102108
103109 See ":ref: `twig-reference-form-variables `" to learn about the ``variables ``
104110argument.
@@ -122,8 +128,8 @@ Renders any errors for the given field.
122128form_widget(view, variables)
123129----------------------------
124130
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.
127133
128134.. code-block :: jinja
129135
@@ -181,8 +187,8 @@ form_enctype(view)
181187
182188.. note ::
183189
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.
186192
187193If the form contains at least one file upload field, this will render the
188194required ``enctype="multipart/form-data" `` form attribute. It's always a
@@ -204,7 +210,8 @@ selectedchoice(selected_value)
204210~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
205211
206212This 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).
208215
209216.. code-block :: jinja
210217
@@ -221,7 +228,7 @@ More about Form Variables
221228
222229In almost every Twig function above, the final argument is an array of "variables"
223230that 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
225232to include a special class:
226233
227234.. code-block :: jinja
@@ -242,41 +249,52 @@ Look at the ``form_label`` as an example:
242249 {% if not compound %}
243250 {% set label_attr = label_attr|merge({'for': id}) %}
244251 {% endif %}
252+
245253 {% 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+ }) %}
247257 {% endif %}
258+
248259 {% if label is empty %}
249260 {% set label = name|humanize %}
250261 {% 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>
252270 {% endblock form_label %}
253271
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).
259277
260278The exact variables available to override depends on which part of the form
261279you'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.
265283
266284.. tip ::
267285
268286 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.
273291
274292.. note ::
275293
276294 If you're rendering an entire form at once (or an entire embedded form),
277295 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:
280298
281299 .. code-block :: jinja
282300
@@ -292,10 +310,10 @@ The following variables are common to every field type. Certain field types
292310may have even more variables and some variables here only really apply to
293311certain types.
294312
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:
299317
300318.. configuration-block ::
301319
0 commit comments