Installation
Add the bundle to your composer.json:
composer require drosalys-web/form-bundle
Enable it in config/bundles.php:
return [
// ...
Drosalys\Web\FormBundle\FormBundle::class => ['all' => true],
];
Basic Form Usage
Extend Symfony’s AbstractType to leverage the bundle’s features:
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Drosalys\Web\FormBundle\Form\Type\ExtendedFormType;
class MyFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('field_name', ExtendedFormType::class, [
'attr' => ['class' => 'custom-class'],
'label' => 'Custom Label',
'required' => true,
]);
}
}
First Use Case: Dynamic Field Attributes
Use the bundle’s ExtendedFormType to inject dynamic attributes (e.g., data-* or class):
{{ form_start(form) }}
{{ form_widget(form.field_name) }} {# Renders with injected attributes #}
{{ form_end(form) }}
ExtendedFormType:
$builder->add('email', ExtendedFormType::class, [
'attr' => ['data-validation' => 'email'],
]);
templates/form/ (e.g., form_div_layout.html.twig) to modify rendering.use Drosalys\Web\FormBundle\Validator\Constraints\CustomConstraint;
$builder->add('username', TextType::class, [
'constraints' => [new CustomConstraint()],
]);
form_errors Twig function for consistent error display:
{% form_errors form.field_name %}
use Drosalys\Web\FormBundle\Event\FormEvents;
$eventDispatcher->addListener(FormEvents::PRE_SUBMIT, function ($event) {
$data = $event->getData();
// Modify data before submission
});
use Drosalys\Web\FormBundle\Event\FormEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class FormLoggerSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [FormEvents::POST_SUBMIT => 'logSubmission'];
}
}
Form component (if using Laravel’s Symfony bridge):
use Symfony\Component\Form\FormFactoryInterface;
$formFactory = app(FormFactoryInterface::class);
$form = $formFactory->create(MyFormType::class);
NotBlank → required).Template Overrides
form_div_layout.html.twig). Misnaming breaks rendering.config/packages/drosalys_web_form.yaml.Event Dispatcher Conflicts
EventDispatcher is not overridden.Event facade for Laravel-specific events and Symfony’s for form events.Constraint Validation
Constraint interface. Forgetting this causes silent failures.AbstractConstraint for validation logic.Form dumper for debugging:
$form->dump();
{% if app.debug %}{% form_theme form _self %}{% endif %}
Custom Field Types
Extend ExtendedFormType to create reusable field types:
class CustomSelectType extends ExtendedFormType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['choices' => []]);
}
}
Form Themes
Create a custom theme directory (templates/form/) and reference it in Twig:
{% form_theme form ['form_theme.html.twig'] %}
Configuration
Override bundle settings in config/packages/drosalys_web_form.yaml:
drosalys_web_form:
default_attr: { class: 'form-control' }
# config/packages/framework.yaml
form:
enabled: true
cache: true
How can I help you explore Laravel packages today?