Installation Add the bundle via Composer:
composer require c33s/form-extra-bundle
Register the bundle in config/bundles.php:
return [
// ...
C33s\FormExtraBundle\FormExtraBundle::class => ['all' => true],
];
First Use Case: Disable HTML5 Validation Extend your form type and disable HTML5 validation globally or per field:
use C33s\FormExtraBundle\Form\Type\Extension\Html5ValidationExtension;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class MyFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addExtension(new Html5ValidationExtension(false)); // Disable HTML5 validation
}
}
Key Configuration
Check config/packages/c33s_form_extra.yaml (if provided) for default settings. If not, configure via DI:
# config/services.yaml
services:
C33s\FormExtraBundle\:
resource: '../vendor/c33s/form-extra-bundle/src/*'
exclude: '../vendor/c33s/form-extra-bundle/src/{Entity,Tests,DependencyInjection}'
Disabling HTML5 Validation
// src/EventListener/FormExtensionListener.php
use C33s\FormExtraBundle\Form\Type\Extension\Html5ValidationExtension;
public function onKernelRequest(GetResponseEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$container = $event->getKernel()->getContainer();
$container->get('form.type_extension.html5_validation')->setEnabled(false);
}
html5_validation: false in add():
$builder->add('field', TextType::class, [
'html5_validation' => false,
]);
Testing Forms Without HTML5 Validation
Html5ValidationExtension in PHPUnit tests to mock validation behavior:
$form = $this->factory->createBuilder(MyFormType::class)
->addExtension(new Html5ValidationExtension(false))
->getForm();
Integration with Symfony Forms
// src/DependencyInjection/Compiler/FormExtensionsPass.php
use C33s\FormExtraBundle\Form\Type\Extension\Html5ValidationExtension;
public function process(ContainerBuilder $container)
{
$definition = $container->findDefinition('form.type_extension.html5_validation');
$definition->setArgument(0, false); // Disable by default
}
Custom Validation Logic
Constraints or custom validators:
use Symfony\Component\Validator\Constraints as Assert;
$builder->add('email', EmailType::class, [
'constraints' => [
new Assert\NotBlank(),
new Assert\Email(),
],
'html5_validation' => false, // Override HTML5 email validation
]);
Extension Registration Order
FormThemeExtension), ensure Html5ValidationExtension is registered last to override defaults.Caching Issues
onKernelRequest) may not affect cached forms.php bin/console cache:clear
Field-Specific Overrides
html5_validation: false on a field does not disable parent form-level HTML5 validation.Testing Edge Cases
Symfony\Bundle\FrameworkBundle\Test\WebTestCase) may reset extensions between tests.setUp():
protected function setUp(): void
{
parent::setUp();
$this->formFactory->addExtension(new Html5ValidationExtension(false));
}
Debugging Validation
vars to check if html5_validation is applied:
$form->getConfig()->getOption('html5_validation'); // Should return false
Performance
Extension Points
Html5ValidationExtension to add custom logic:
class CustomHtml5ValidationExtension extends Html5ValidationExtension
{
public function __construct(bool $enabled, array $customRules = [])
{
parent::__construct($enabled);
$this->customRules = $customRules;
}
}
Symfony 6+ Compatibility
form.type_extension service is tagged correctly:
services:
C33s\FormExtraBundle\Form\Type\Extension\Html5ValidationExtension:
tags: ['form.type_extension']
Browser Testing
How can I help you explore Laravel packages today?