ecohead/symfony-tailwind-form-theme-bundle
Installation:
composer require ecohead/symfony-tailwind-form-theme-bundle
For non-Flex projects, manually register the bundle in config/bundles.php:
Ecohead\TailwindFormTheme\EcoheadTailwindFormThemeBundle::class => ['all' => true],
Enable Tailwind Theme:
Add the theme to your form types in config/packages/twig.yaml:
twig:
form_themes: ['@EcoheadTailwindFormTheme/form/theme.html.twig']
First Use Case: Use the bundle in a form type:
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class MyFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', TextType::class, [
'attr' => ['class' => 'w-full p-2 border rounded'],
]);
}
}
Render the form in Twig:
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
Form Customization:
Override the default theme by extending the base template (theme.html.twig) in your project’s templates/form/ directory.
Example: Copy @EcoheadTailwindFormTheme/form/theme.html.twig to templates/form/tailwind_theme.html.twig and customize.
Dynamic Class Assignment:
Use the attr option to add Tailwind classes to form fields:
$builder->add('email', EmailType::class, [
'attr' => ['class' => 'mt-4 p-3 border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500'],
]);
Integration with Symfony UX: Combine with Symfony UX Turbo for dynamic form updates:
{{ form_start(form, {'attr': {'data-turbo-frame': '_self'}}) }}
{{ form_widget(form) }}
{{ form_end(form) }}
Reusable Form Blocks: Create modular form components in Twig:
{# templates/components/input.html.twig #}
<div class="mb-4">
{{ form_label(form) }}
{{ form_widget(form) }}
{{ form_errors(form) }}
</div>
tailwind.config.js includes form-related classes (e.g., focus:ring-*, border-*).PRE_SET_DATA or POST_SUBMIT to dynamically adjust form attributes:
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$event->getForm()->add('dynamic_field', TextType::class, [
'attr' => ['class' => 'bg-red-100'],
]);
});
Theme Override Conflicts:
{# templates/form/tailwind_theme.html.twig #}
{% extends '@EcoheadTailwindFormTheme/form/theme.html.twig' %}
form_row or form_widget without extending them.Tailwind Class Mismatches:
focus:ring-blue-500 requires @focus-ring-color in tailwind.config.js).Bundle Registration:
bundles.php or use AppKernel.php.Form Theme Caching:
php bin/console cache:clear
{{ dump(form|render|raw) }} in Twig to debug form structure.Custom Form Types:
Extend the bundle’s theme for custom form types (e.g., ChoiceType):
{# templates/form/choice_widget.html.twig #}
{% extends '@EcoheadTailwindFormTheme/form/choice_widget.html.twig' %}
{% block choice_widget_options %}
{{ parent() }} class="shadow appearance-none border rounded w-full py-2 px-3"
{% endblock %}
Dynamic Theming:
Use Twig’s form_theme function to switch themes conditionally:
{% form_theme form with ['@EcoheadTailwindFormTheme/form/theme.html.twig'] %}
Symfony 6+ Compatibility:
Test with Symfony’s new form component if migrating:
use Symfony\Component\Form\FormInterface;
$form->getConfig()->getTheme()->addPath('@EcoheadTailwindFormTheme');
How can I help you explore Laravel packages today?