Weave Code
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 Extensions Laravel Package

sonata-project/form-extensions

Additional form types, data transformers, and utilities for Symfony Form via Sonata. Includes date/time and choice helpers, improved widgets, and integration-friendly extensions to speed up building consistent, reusable form components across projects.

View on GitHub
Deep Wiki
Context7
## Getting Started

### Minimal Setup
1. **Installation**
   Add the package via Composer (ensure compatibility with Symfony 8+ and PHP 8.2+):
   ```bash
   composer require sonata-project/form-extensions:^2.7.0

Register the service provider in config/app.php (Laravel-Symfony bridge required):

'providers' => [
    // ...
    SonataProject\Form\Extensions\Provider\SonataFormExtensionsServiceProvider::class,
],
  1. Basic Usage Extend Symfony form types with Sonata’s extensions (e.g., CollectionType):

    use Sonata\Form\Extensions\Type\CollectionType;
    use Sonata\Form\Extensions\Type\ChoiceType;
    
    $builder
        ->add('tags', CollectionType::class, [
            'entry_type' => ChoiceType::class,
            'entry_options' => ['choices' => $tagChoices],
        ]);
    
  2. First Use Case Replace nested forms with Sonata’s CollectionType for dynamic inline editing:

    $builder->add('items', CollectionType::class, [
        'prototype' => true, // Enables inline editing
        'allow_add' => true,
        'allow_delete' => true,
        'entry_type' => TextType::class, // Symfony 8+ compatible
    ]);
    

Implementation Patterns

Common Workflows

  1. Dynamic Collections with Symfony 8 Use CollectionType for variable-length arrays (e.g., user roles) with Symfony 8’s form components:

    $builder->add('roles', CollectionType::class, [
        'entry_type' => EntityType::class,
        'entry_options' => ['class' => UserRole::class],
        'by_reference' => false, // Critical for detached entities
        'prototype' => true,
    ]);
    
  2. Conditional Fields Leverage ConditionalFieldExtension to toggle fields dynamically:

    $builder->add('shipping', TextType::class, [
        'conditionally_enabled' => [
            'field' => 'needs_shipping',
            'value' => true,
        ],
    ]);
    
  3. Grouped Fields Organize fields into collapsible sections with GroupType:

    $builder->add('billing', GroupType::class, [
        'fields' => ['address', 'city', 'zip'],
        'label' => 'Billing Information',
        'attr' => ['class' => 'collapsible'], // Symfony 8+ supports `attr`
    ]);
    

Integration Tips

  • Symfony 8 Form Factory Use Laravel’s service container to instantiate Symfony 8’s FormFactory:

    $formFactory = app(Symfony\Component\Form\FormFactoryInterface::class);
    $form = $formFactory->createNamedBuilder('dynamic_form', null, null)->getForm();
    
  • Twig Integration Extend Twig templates with Sonata’s themes (ensure compatibility with Symfony 8’s Twig bundle):

    {% extends 'SonataForm::standard_layout.html.twig' %}
    {# Override in resources/views/vendor/sonata_form/ #}
    
  • Validation with Symfony 8 Combine with Symfony 8’s validators (e.g., Callback, Expression):

    use Symfony\Component\Validator\Constraints\Callback;
    
    $builder->add('age', IntegerType::class, [
        'constraints' => [new Callback([$validator, 'validateAge'])],
    ]);
    

Gotchas and Tips

Pitfalls

  1. PHP/Symfony Version Mismatch

    • Issue: Package now requires PHP 8.2+ and Symfony 8+. Older versions (e.g., PHP 8.1, Symfony 7.x) are dropped.
    • Fix: Update dependencies in composer.json:
      {
          "require": {
              "php": "^8.2",
              "symfony/form": "^8.0",
              "symfony/twig-bundle": "^8.0"
          }
      }
      
  2. Prototype Cloning in Symfony 8

    • Issue: prototype: true may cause duplicate IDs in Symfony 8’s stricter DOM handling.
    • Fix: Use prototype_name with a unique prefix:
      'prototype_name' => '__item__', // Avoids ID collisions
      
  3. CSRF in AJAX (Symfony 8)

    • Issue: Dynamic forms may fail CSRF validation in Symfony 8’s updated security layer.
    • Fix: Include _token in AJAX requests or use Symfony’s CsrfTokenManager:
      {{ form_start(form, {'attr': {'id': 'dynamic-form'}}) }}
      {{ form_hidden(form.csrf_token) }} {# Explicit token #}
      
  4. By-Reference in Collections

    • Issue: by_reference: true can detach entities in Symfony 8’s ORM integration.
    • Fix: Set by_reference: false or use Symfony\Bridge\Doctrine\Form\Type\EntityType:
      'entry_options' => ['class' => User::class, 'by_reference' => false],
      

Debugging

  • Symfony 8 Debug Tools Use Symfony’s DebugForm component to inspect form data:

    use Symfony\Component\Form\Extension\Core\Type\FormType;
    
    $form = $formFactory->create(FormType::class);
    $form->submit($data);
    dump($form->getNormData()); // Debug normalized data
    
  • Event Listeners (Symfony 8) Debug form events with Symfony 8’s updated event system:

    use Symfony\Component\Form\FormEvents;
    
    $builder->addEventListener(FormEvents::PRE_SET_DATA, function ($event) {
        dump($event->getData()); // Symfony 8+ event object
    });
    

Extension Points

  1. Custom Types for Symfony 8 Extend AbstractType with Symfony 8’s form components:

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    
    class CustomCollectionType extends AbstractType {
        public function buildForm(FormBuilderInterface $builder, array $options) {
            $builder->add('items', CollectionType::class, [
                'entry_type' => $options['entry_type'],
                'prototype' => true,
            ]);
        }
    }
    
  2. Override Templates (Symfony 8) Override Sonata’s Twig templates in resources/views/vendor/sonata_form/:

    {# resources/views/vendor/sonata_form/field/collection.html.twig #}
    {% block sonata_form_collection_prototype %}
        <div class="collection-prototype symfony-8-compatible">
            {{ form_row(form) }}
        </div>
    {% endblock %}
    
  3. Configuration Quirks

    • Symfony 8 Defaults: Sonata’s types now align with Symfony 8’s stricter defaults. Explicitly override:
      'allow_add' => true, // Symfony 8 defaults to `false`
      'allow_delete' => true,
      'error_bubbling' => true, // Symfony 8+ best practice
      
    • Deprecated Options: Remove Symfony 7.x-specific options (e.g., error_mapping).

---
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.
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
directorytree/privacy-filter-classifier
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi