.. index:: single: Forms; Theming single: Forms; Customizing fields How to Work with Form Themes ============================ This article explains how to use in your app any of the form themes provided by Symfony and how to create your own custom form theme. .. _symfony-builtin-forms: Symfony Built-In Form Themes ---------------------------- Symfony comes with several **built-in form themes** that make your forms look great when using some of the most popular CSS frameworks. Each theme is defined in a single Twig template and they are enabled in the :ref:`twig.form_themes ` option: * `form_div_layout.html.twig`_, wraps each form field inside a ``
`` element and it's the theme used by default in Symfony applications unless you configure it as explained later in this article. * `form_table_layout.html.twig`_, wraps the entire form inside a ```` element and each form field inside a ```` element. * `bootstrap_3_layout.html.twig`_, wraps each form field inside a ``
`` element with the appropriate CSS classes to apply the styles used by the `Bootstrap 3 CSS framework`_. * `bootstrap_3_horizontal_layout.html.twig`_, it's similar to the previous theme, but the CSS classes applied are the ones used to display the forms horizontally (i.e. the label and the widget in the same row). * `bootstrap_4_layout.html.twig`_, same as ``bootstrap_3_layout.html.twig``, but updated for `Bootstrap 4 CSS framework`_ styles. * `bootstrap_4_horizontal_layout.html.twig`_, same as ``bootstrap_3_horizontal_layout.html.twig`` but updated for Bootstrap 4 styles. * `foundation_5_layout.html.twig`_, wraps each form field inside a ``
`` element with the appropriate CSS classes to apply the default styles of the version 5 of `Foundation CSS framework`_. * `foundation_6_layout.html.twig`_, wraps each form field inside a ``
`` element with the appropriate CSS classes to apply the default styles of the version 6 of `Foundation CSS framework`_. * `bulma_0_layout.html.twig`_, wraps each form field inside a ``
`` element with the appropriate CSS classes to apply the styles used by the `Bulma CSS framework`_. .. versionadded:: 5.1 The ``foundation_6_layout.html.twig`` and ``bulma_0_layout.html.twig`` themes were introduced in Symfony 5.1. .. tip:: Read the article about the :doc:`Bootstrap 4 Symfony form theme ` to learn more about it. .. _forms-theming-global: .. _forms-theming-twig: Applying Themes to all Forms ---------------------------- Symfony forms use by default the ``form_div_layout.html.twig`` theme. If you want to use another theme for all the forms of your app, configure it in the ``twig.form_themes`` option: .. configuration-block:: .. code-block:: yaml # config/packages/twig.yaml twig: form_themes: ['bootstrap_4_horizontal_layout.html.twig'] # ... .. code-block:: xml bootstrap_4_horizontal_layout.html.twig .. code-block:: php // config/packages/twig.php $container->loadFromExtension('twig', [ 'form_themes' => [ 'bootstrap_4_horizontal_layout.html.twig', ], // ... ]); You can pass multiple themes to this option because sometimes form themes only redefine a few elements. This way, if some theme doesn't override some element, Symfony looks up in the other themes. The order of the themes in the ``twig.form_themes`` option is important. Each theme overrides all the previous themes, so you must put the most important themes at the end of the list. Applying Themes to Single Forms ------------------------------- Although most of the times you'll apply form themes globally, you may need to apply a theme only to some specific form. You can do that with the :ref:`form_theme Twig tag `: .. code-block:: twig {# this form theme will be applied only to the form of this template #} {% form_theme form 'foundation_5_layout.html.twig' %} {{ form_start(form) }} {# ... #} {{ form_end(form) }} The first argument of the ``form_theme`` tag (``form`` in this example) is the name of the variable that stores the form view object. The second argument is the path of the Twig template that defines the form theme. Applying Multiple Themes to Single Forms ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ A form can also be customized by applying several themes. To do this, pass the path of all the Twig templates as an array using the ``with`` keyword (their order is important, because each theme overrides all the previous ones): .. code-block:: twig {# apply multiple form themes but only to the form of this template #} {% form_theme form with [ 'foundation_5_layout.html.twig', 'forms/my_custom_theme.html.twig' ] %} {# ... #} Applying Different Themes to Child Forms ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can also apply a form theme to a specific child of your form: .. code-block:: twig {% form_theme form.a_child_form 'form/my_custom_theme.html.twig' %} This is useful when you want to have a custom theme for a nested form that's different than the one of your main form. Specify both your themes: .. code-block:: twig {% form_theme form 'form/my_custom_theme.html.twig' %} {% form_theme form.a_child_form 'form/my_other_theme.html.twig' %} .. _disabling-global-themes-for-single-forms: Disabling Global Themes for Single Forms ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Global form themes defined in the app are always applied to all forms, even those which use the ``form_theme`` tag to apply their own themes. You may want to disable this for example when creating an admin interface for a bundle which can be installed on different Symfony applications (and so you can't control what themes are enabled globally). To do that, add the ``only`` keyword after the list of form themes: .. code-block:: twig {% form_theme form with ['foundation_5_layout.html.twig'] only %} {# ... #} .. caution:: When using the ``only`` keyword, none of Symfony's built-in form themes (``form_div_layout.html.twig``, etc.) will be applied. In order to render your forms correctly, you need to either provide a fully-featured form theme yourself, or extend one of the built-in form themes with Twig's ``use`` keyword instead of ``extends`` to re-use the original theme contents. .. code-block:: twig {# templates/form/common.html.twig #} {% use "form_div_layout.html.twig" %} {# ... #} .. _create-your-own-form-theme: Creating your Own Form Theme ---------------------------- Symfony uses Twig blocks to render each part of a form - field labels, errors, ```` text fields, `` Symfony uses a Twig block called ``integer_widget`` to render that field. This is because the field type is ``integer`` and you're rendering its ``widget`` (as opposed to its ``label`` or ``errors`` or ``help``). The first step to create a form theme is to know which Twig block to override, as explained in the following section. .. _form-customization-sidebar: .. _form-fragment-naming: Form Fragment Naming ~~~~~~~~~~~~~~~~~~~~ The naming of form fragments varies depending on your needs: * If you want to customize **all fields of the same type** (e.g. all ``