Skip to content

Commit b774bc2

Browse files
committed
[#1039] Tweaks thanks to @stof and @wouterj
1 parent 7a124d8 commit b774bc2

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

reference/forms/twig_reference.rst

+25-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ good idea to include this in your form tag:
132132
More about Form Variables
133133
-------------------------
134134

135-
For a full list of variables, see: :ref:`reference-form-twig-variables`.
135+
.. tip::
136+
137+
For a full list of variables, see: :ref:`reference-form-twig-variables`.
136138

137139
In almost every Twig function above, the final argument is an array of "variables"
138140
that are used when rendering that one part of the form. For example, the
@@ -200,6 +202,27 @@ The following variables are common to every field type. Certain field types
200202
may have even more variables and some variables here only really apply to
201203
certain types.
202204

205+
Assuming you have a ``form`` variable in your template, and you want to reference
206+
the variables on the ``name`` field, accessing the variables is done by using
207+
a public ``vars`` property on the :class:`Symfony\\Component\\Form\\FormView`
208+
object:
209+
210+
.. configuration-block::
211+
212+
.. code-block:: html+jinja
213+
214+
<label for="{{ form.name.vars.id }}"
215+
class="{{ form.name.vars.required ? 'required' : '' }}">
216+
{{ form.name.label }}
217+
</label>
218+
219+
.. code-block:: html+php
220+
221+
<label for="<?php echo $view['form']->get('name')->vars['id'] ?>"
222+
class="<?php echo $view['form']->get('name')->vars['required'] ? 'required' : '' ?>">
223+
<?php echo $view['form']->get('name')->vars['label'] ?>
224+
</label>
225+
203226
+-----------------+-----------------------------------------------------------------------------------------+
204227
| Variable | Usage |
205228
+=================+=========================================================================================+
@@ -228,7 +251,7 @@ certain types.
228251
| ``label`` | The string label that will be rendered |
229252
+-----------------+-----------------------------------------------------------------------------------------+
230253
| ``multipart`` | If ``true``, ``form_enctype`` will render ``enctype="multipart/form-data"``. |
231-
| | This only applies to the parent form element. |
254+
| | This only applies to the root form element. |
232255
+-----------------+-----------------------------------------------------------------------------------------+
233256
| ``attr`` | A key-value array that will be rendered as HTML attributes on the field |
234257
+-----------------+-----------------------------------------------------------------------------------------+

reference/map.rst.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
* :doc:`Form Field Type Reference</reference/forms/types>`
2020
* :doc:`Validation Constraints Reference </reference/constraints>`
21-
* :doc:`Twig Template Function Reference</reference/forms/twig_reference>`
21+
* :doc:`Twig Template Function and Variable Reference</reference/forms/twig_reference>`
2222

2323
* :doc:`Twig Extensions (forms, filters, tags, etc) Reference</reference/twig_reference>`
2424

0 commit comments

Comments
 (0)