@@ -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,75 @@ 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 ``form_label `` as an example:
124
+
125
+ .. code-block :: jinja
126
+
127
+ {% block form_label %}
128
+ {% if not compound %}
129
+ {% set label_attr = label_attr|merge({'for': id}) %}
130
+ {% endif %}
131
+ {% if required %}
132
+ {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
133
+ {% endif %}
134
+ {% if label is empty %}
135
+ {% set label = name|humanize %}
136
+ {% endif %}
137
+ <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
138
+ {% endblock form_label %}
139
+
140
+ This block makes use of several variables: ``compound ``, ``label_attr ``, ``required ``,
141
+ ``label ``, ``name `` and ``translation_domain ``.
142
+ These variables are made available by the form rendering system. But more
143
+ importantly, these are the variables that you can override when calling ``form_label ``
144
+ (since in this example, you're rendering the label).
145
+
146
+ The exact variables available to override depends on which part of the form
147
+ you're rendering (e.g. label versus widget) and which field you're rendering
148
+ (e.g. a ``choice `` widget has an extra ``expanded `` option). If you get comfortable
149
+ with looking through `form_div_layout.html.twig `_, you'll always be able
150
+ to see what options you have available.
151
+
152
+ .. tip ::
153
+
154
+ Behind the scenes, these variables are made available to the ``FormView ``
155
+ object of your form when the form component calls ``buildView `` and ``buildViewBottomUp ``
156
+ on each "node" of your form tree. To see what "view" variables a particularly
157
+ field has, find the source code for the form field (and its parent fields)
158
+ and look at the above two functions.
159
+
160
+ .. note ::
161
+
162
+ If you're rendering an entire form at once (or an entire embedded form),
163
+ the ``variables `` argument will only be applied to the form itself and
164
+ not its children. In other words, the following will **not ** pass a "foo"
165
+ class attribute to all of the child fields in the form:
166
+
167
+ .. code-block :: jinja
168
+
169
+ {# does **not** work - the variables are not recursive #}
170
+ {{ form_widget(form, { 'attr': {'class': 'foo'} }) }}
171
+
172
+ .. _`form_div_layout.html.twig` : https://github.com/symfony/symfony/blob/2.1/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
0 commit comments