Skip to content

[Toolkit] Provide a Symfony Form theme for each kit #3755

Description

@seb-jean

Description

The Toolkit kits (bootstrap, flowbite-4, shadcn) ship ready-to-use Twig UI components, but nothing for Symfony Forms. As soon as an application renders a form with form_row() / form_widget(), the output falls back to Symfony's default markup and looks completely out of place next to the kit's components.
Everyone ends up writing (and maintaining) the same form theme by hand.

I'd like each kit to provide a recipe that installs a
templates/form/theme.html.twig file (or any other file name), built on top of form_div_layout.html.twig:

{% use 'form_div_layout.html.twig' %}

{% block form_label %}{# kit-specific markup #}{% endblock %}
{% block form_widget_simple %}{# ... #}{% endblock %}
{% block form_errors %}{# ... #}{% endblock %}
{% block form_help %}{# ... #}{% endblock %}

Installed the usual way:

php bin/console ux:install form-theme

and enabled in config/packages/twig.yaml:

twig:
    form_themes:
        - 'form/theme.html.twig' # adjust to the chosen file name

Starting from form_div_layout.html.twig keeps the theme minimal and predictable: only the blocks that need kit-specific classes are overridden, everything else keeps Symfony's default behaviour.

Note

One thing to keep in mind: the attribute-related blocks of form_div_layout.html.twig : widget_attributes, widget_container_attributes, button_attributes and attributes - render an already-serialized HTML string, not a structured set of attributes. So they can't be passed as-is to a Twig component (<twig:Input {{ block('widget_attributes') }} /> won't work), and adding a class means either merging into the attr variable before those blocks run, or overriding them entirely.

Open questions

  • bootstrap kit: Symfony already ships bootstrap_5_layout.html.twig. Should the kit's theme extend it instead of form_div_layout.html.twig, or stay consistent with the other kits?
  • Does the recipe installer already allow copying files outside templates/components/ (via copy-files)? If so, no change is needed there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFCRFC = Request For Comments (proposals about features that you want to be discussed)Toolkit

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions