Skip to content

Commit 2b5ae1c

Browse files
committed
Add some basic docs for Twig Form field helpers
1 parent aadac73 commit 2b5ae1c

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

form/form_customization.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,43 @@ control over how each form field is rendered, so you can fully customize them:
8787
Later in this article you can find the full reference of these Twig
8888
functions with more usage examples.
8989

90+
Form Field Helpers
91+
------------------
92+
93+
The ``form_*()`` helpers render each part of the form field, including all its needed HTML elements. Most developers
94+
like this behavior, but some designers struggle with it, because it hides all the HTML in form themes which are not
95+
easy to manage by them.
96+
97+
That's why some Twig form helpers are available to render the value of each form field part without adding any
98+
HTML around it:
99+
100+
* ``field_name``
101+
* ``field_value``
102+
* ``field_label``
103+
* ``field_help``
104+
* ``field_errors``
105+
* ``field_choices`` (an iterator of the field choices; e.g. for ``<select>``)
106+
107+
When using these helpers, you must write all the HTML contents for all form fields, which some people prefer to better
108+
control the generated HTML without having to deal with form themes:
109+
110+
.. code-block:: html+twig
111+
112+
<input
113+
name="{{ field_name(form.username) }}"
114+
value="{{ field_value(form.username) }}"
115+
placeholder="{{ field_label(form.username) }}"
116+
class="form-control"
117+
/>
118+
119+
<select name="{{ field_name(form.country) }}" class="form-control">
120+
<option value="">{{ field_label(form.country) }}</option>
121+
122+
{% for label, value in field_choices(form.country) %}
123+
<option value="{{ value }}">{{ label }}</option>
124+
{% endfor %}
125+
</select>
126+
90127
Form Rendering Variables
91128
------------------------
92129

reference/twig_reference.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,12 @@ explained in the article about :doc:`customizing form rendering </form/form_cust
365365
* :ref:`form_help() <reference-forms-twig-help>`
366366
* :ref:`form_row() <reference-forms-twig-row>`
367367
* :ref:`form_rest() <reference-forms-twig-rest>`
368+
* :ref:`field_name() <reference-forms-twig-rest>`
369+
* :ref:`field_value() <reference-forms-twig-rest>`
370+
* :ref:`field_label() <reference-forms-twig-rest>`
371+
* :ref:`field_help() <reference-forms-twig-rest>`
372+
* :ref:`field_errors() <reference-forms-twig-rest>`
373+
* :ref:`field_choices() <reference-forms-twig-rest>`
368374

369375
.. _reference-twig-filters:
370376

0 commit comments

Comments
 (0)