|
1 | 1 | .. index::
|
2 | 2 | single: Forms; Twig form function reference
|
3 | 3 |
|
4 |
| -Twig Template Form Function Reference |
5 |
| -===================================== |
| 4 | +Twig Template Form Function and Variable Reference |
| 5 | +================================================== |
| 6 | + |
| 7 | +When working with forms in a template, there are two powerful things at your |
| 8 | +disposal: |
| 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 |
| 12 | + |
| 13 | +You'll use functions often to render your fields. Variables, on the other |
| 14 | +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. |
| 16 | + |
| 17 | +.. _reference-form-twig-functions: |
| 18 | + |
| 19 | +Form Rendering Functions |
| 20 | +------------------------ |
6 | 21 |
|
7 | 22 | This reference manual covers all the possible Twig functions available for
|
8 | 23 | rendering forms. There are several different functions available, and each
|
@@ -114,8 +129,10 @@ good idea to include this in your form tag:
|
114 | 129 |
|
115 | 130 | .. _`twig-reference-form-variables`:
|
116 | 131 |
|
117 |
| -More about Form "Variables" |
118 |
| ---------------------------- |
| 132 | +More about Form Variables |
| 133 | +------------------------- |
| 134 | + |
| 135 | +For a full list of variables, see: :ref:`reference-form-twig-variables`. |
119 | 136 |
|
120 | 137 | In almost every Twig function above, the final argument is an array of "variables"
|
121 | 138 | that are used when rendering that one part of the form. For example, the
|
@@ -174,4 +191,46 @@ to see what options you have available.
|
174 | 191 | {# does **not** work - the variables are not recursive #}
|
175 | 192 | {{ form_widget(form, { 'attr': {'class': 'foo'} }) }}
|
176 | 193 |
|
| 194 | +.. _reference-form-twig-variables: |
| 195 | + |
| 196 | +Form Variables Reference |
| 197 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 198 | + |
| 199 | +The following variables are common to every field type. Certain field types |
| 200 | +may have even more variables and some variables here only really apply to |
| 201 | +certain types. |
| 202 | + |
| 203 | ++-----------------+-----------------------------------------------------------------------------------------+ |
| 204 | +| Variable | Usage | |
| 205 | ++=================+=========================================================================================+ |
| 206 | +| ``id`` | The ``id`` HTML attribute to be rendered | |
| 207 | ++-----------------+-----------------------------------------------------------------------------------------+ |
| 208 | +| ``name`` | The name of the field (e.g. ``title``) - but not the ``name`` | |
| 209 | +| | HTML attribute, which is ``full_name`` | |
| 210 | ++-----------------+-----------------------------------------------------------------------------------------+ |
| 211 | +| ``full_name`` | The ``name`` HTML attribute to be rendered | |
| 212 | ++-----------------+-----------------------------------------------------------------------------------------+ |
| 213 | +| ``errors`` | An array of any errors attached to *this* specific field (e.g. ``form.title.errors``). | |
| 214 | +| | Note that you can't use ``form.errors`` to determine if a form is valid, | |
| 215 | +| | since this only returns "global" errors: some individual fields may have errors | |
| 216 | ++-----------------+-----------------------------------------------------------------------------------------+ |
| 217 | +| ``value`` | The value that will be used when rendering (commonly the ``value`` HTML attribute) | |
| 218 | ++-----------------+-----------------------------------------------------------------------------------------+ |
| 219 | +| ``read_only`` | If ``true``, ``disabled="disabled"`` is added to the field | |
| 220 | ++-----------------+-----------------------------------------------------------------------------------------+ |
| 221 | +| ``required`` | If ``true``, a ``required`` attribute is added to the field to activate HTML5 | |
| 222 | +| | validation. Additionally, a ``required`` class is added to the label. | |
| 223 | ++-----------------+-----------------------------------------------------------------------------------------+ |
| 224 | +| ``max_length`` | Adds a ``maxlength`` HTML attribute to the element | |
| 225 | ++-----------------+-----------------------------------------------------------------------------------------+ |
| 226 | +| ``pattern`` | Adds a ``pattern`` HTML attribute to the element | |
| 227 | ++-----------------+-----------------------------------------------------------------------------------------+ |
| 228 | +| ``label`` | The string label that will be rendered | |
| 229 | ++-----------------+-----------------------------------------------------------------------------------------+ |
| 230 | +| ``multipart`` | If ``true``, ``form_enctype`` will render ``enctype="multipart/form-data"``. | |
| 231 | +| | This only applies to the parent form element. | |
| 232 | ++-----------------+-----------------------------------------------------------------------------------------+ |
| 233 | +| ``attr`` | A key-value array that will be rendered as HTML attributes on the field | |
| 234 | ++-----------------+-----------------------------------------------------------------------------------------+ |
| 235 | + |
177 | 236 | .. _`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