Skip to content

[WIP][Form] Implement Twig helpers to get field variables #14308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Form] Implement Twig helpers to get field variables
  • Loading branch information
tgalopin committed Sep 30, 2020
commit 219e3acd11d7afb480bba77ac1adb79504d4eae8
27 changes: 24 additions & 3 deletions form/form_customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,30 @@ enough to render an entire form, including all its fields and error messages:
by calling to the $form->createView() method #}
{{ form(form) }}

The next step is to use the :ref:`form_start() <reference-forms-twig-start>`,
As short as this rendering is, it's not very flexible. The next step is to use the
:ref:`form_start() <reference-forms-twig-start>`,
:ref:`form_end() <reference-forms-twig-end>`,
:ref:`form_errors() <reference-forms-twig-errors>` and
:ref:`form_row() <reference-forms-twig-row>` Twig functions to render the
:ref:`field_name() <reference-forms-twig-field-name>`,
:ref:`field_value() <reference-forms-twig-field-value>` and
:ref:`field_errors() <reference-forms-twig-field-errors>` Twig functions to render the
different form parts so you can customize them adding HTML elements and attributes:

// TODO

Generic Form Rendering
----------------------

If you want to let the PHP code defining the structure of the form also describes
how it should be displayed (for instance in the case of a reusable PHP package),
you may want to rely on generic form rendering instead of individual Twig functions.

To do so, you can use the
:ref:`form_row() <reference-forms-twig-row>`,
:ref:`form_widget() <reference-forms-twig-widget>`,
:ref:`form_errors() <reference-forms-twig-errors>` and
:ref:`form_rest() <reference-forms-twig-rest>`
Twig functions to render the form parts generically:

.. code-block:: html+twig

{{ form_start(form) }}
Expand Down Expand Up @@ -74,6 +92,9 @@ control over how each form field is rendered, so you can fully customize them:
</div>
</div>

Using these generic functions to render forms allow PHP code and general form themes to affect
the way your fields will be displayed.

.. note::

Later in this article you can find the full reference of these Twig
Expand Down
6 changes: 6 additions & 0 deletions reference/twig_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ explained in the article about :doc:`customizing form rendering </form/form_cust
* :ref:`form_help() <reference-forms-twig-help>`
* :ref:`form_row() <reference-forms-twig-row>`
* :ref:`form_rest() <reference-forms-twig-rest>`
* :ref:`field_name() <reference-forms-twig-rest>`
* :ref:`field_value() <reference-forms-twig-rest>`
* :ref:`field_label() <reference-forms-twig-rest>`
* :ref:`field_help() <reference-forms-twig-rest>`
* :ref:`field_errors() <reference-forms-twig-rest>`
* :ref:`field_choices() <reference-forms-twig-rest>`

.. _reference-twig-filters:

Expand Down