Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Form Extra Bundle Laravel Package

c33s/form-extra-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps to Begin

  1. 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],
    ];
    
  2. 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
        }
    }
    
  3. 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}'
    

Implementation Patterns

Common Workflows

  1. Disabling HTML5 Validation

    • Globally: Register the extension in your base form type or via a compiler pass.
      // 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);
      }
      
    • Per Field: Pass html5_validation: false in add():
      $builder->add('field', TextType::class, [
          'html5_validation' => false,
      ]);
      
  2. Testing Forms Without HTML5 Validation

    • Use the bundle’s Html5ValidationExtension in PHPUnit tests to mock validation behavior:
      $form = $this->factory->createBuilder(MyFormType::class)
          ->addExtension(new Html5ValidationExtension(false))
          ->getForm();
      
  3. Integration with Symfony Forms

    • Dynamic Extensions: Use a compiler pass to auto-register extensions:
      // 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
      }
      
  4. Custom Validation Logic

    • Combine with Symfony’s 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
      ]);
      

Gotchas and Tips

Pitfalls

  1. Extension Registration Order

    • If HTML5 validation is enabled elsewhere (e.g., Symfony’s FormThemeExtension), ensure Html5ValidationExtension is registered last to override defaults.
    • Fix: Use a high priority in compiler passes or manually reorder extensions.
  2. Caching Issues

    • Disabling HTML5 validation dynamically (e.g., via onKernelRequest) may not affect cached forms.
    • Fix: Clear the cache after changes:
      php bin/console cache:clear
      
  3. Field-Specific Overrides

    • Setting html5_validation: false on a field does not disable parent form-level HTML5 validation.
    • Fix: Use the extension on the root form type or disable it globally.
  4. Testing Edge Cases

    • Some test frameworks (e.g., PHPUnit with Symfony\Bundle\FrameworkBundle\Test\WebTestCase) may reset extensions between tests.
    • Fix: Re-register extensions in setUp():
      protected function setUp(): void
      {
          parent::setUp();
          $this->formFactory->addExtension(new Html5ValidationExtension(false));
      }
      

Tips

  1. Debugging Validation

    • Inspect the form’s vars to check if html5_validation is applied:
      $form->getConfig()->getOption('html5_validation'); // Should return false
      
  2. Performance

    • Disabling HTML5 validation reduces client-side validation but may increase server-side load if validation is handled manually.
  3. Extension Points

    • The bundle is designed for extension. Override Html5ValidationExtension to add custom logic:
      class CustomHtml5ValidationExtension extends Html5ValidationExtension
      {
          public function __construct(bool $enabled, array $customRules = [])
          {
              parent::__construct($enabled);
              $this->customRules = $customRules;
          }
      }
      
  4. Symfony 6+ Compatibility

    • If using Symfony 6, ensure the bundle’s form.type_extension service is tagged correctly:
      services:
          C33s\FormExtraBundle\Form\Type\Extension\Html5ValidationExtension:
              tags: ['form.type_extension']
      
  5. Browser Testing

    • Use tools like BrowserStack or Laravel Dusk to verify HTML5 validation behavior across browsers when toggling the feature.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
roundly-consulting/kubernetes-api-for-laravel
roundly-consulting/plausible-for-laravel
gvp/gvp-laravel-template
metasyncsite/laravel-metasync-client
roundly-consulting/onboarding-for-laravel
andydefer/laravel-notification
adachsoft/cli-process-tester
jhaskell13/laraflow
sebastienheyd/boilerplate-media-manager
ccast/tagixo-primix
pretendtrue/laravel-builder
sawmainek/laravel-scout-tntsearch-driver
tomshaw/laravel-calendar-table
dibakar/laravel-ownership
steadrun/laravel-monitor
labyrinthe/connect-laravel
elmogy/layan
drewdan/sentinel-client
dskripchenko/laravel-llm
fatryst/nuonuo