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

Symfony Fieldset Bundle Laravel Package

adamquaile/symfony-fieldset-bundle

Adds a FieldsetType to Symfony Forms so you can group fields with a and custom legend. Define fields via a builder callback or a simple array, keeping form structure tidy and reusable.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require adamquaile/symfony-fieldset-bundle
    

    Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3):

    AdamQuaile\Bundle\FieldsetBundle\AdamQuaileFieldsetBundle::class => ['all' => true],
    
  2. First Use Case: Create a form type and add a fieldset with a callback:

    use AdamQuaile\Bundle\FieldsetBundle\Form\Type\FieldsetType;
    
    class MyFormType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('address', FieldsetType::class, [
                'legend' => 'Contact Information',
                'fields' => function(FormBuilderInterface $builder) {
                    $builder->add('street', TextType::class);
                    $builder->add('city', TextType::class);
                }
            ]);
        }
    }
    

Where to Look First

  • Documentation: The README provides basic usage, but explore the source code for advanced options.
  • Symfony Forms: Familiarize yourself with Symfony’s FormBuilderInterface since fieldsets extend it.

Implementation Patterns

Core Workflows

  1. Nested Fieldsets: Fieldsets can contain other fieldsets for hierarchical forms:

    $builder->add('profile', FieldsetType::class, [
        'fields' => function($builder) {
            $builder->add('personal', FieldsetType::class, [
                'fields' => function($builder) {
                    $builder->add('name', TextType::class);
                }
            ]);
        }
    ]);
    
  2. Dynamic Fields: Use closures to conditionally add fields based on runtime logic:

    'fields' => function(FormBuilderInterface $builder) use ($user) {
        if ($user->isAdmin()) {
            $builder->add('admin_options', FieldsetType::class);
        }
    }
    
  3. Reusable Fieldsets: Extract fieldset definitions into separate form types for DRY code:

    class AddressFieldsetType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('street', TextType::class);
            $builder->add('city', TextType::class);
        }
    }
    

    Then reuse:

    $builder->add('address', AddressFieldsetType::class);
    
  4. Array-Based Fields: For static configurations, use the array syntax:

    'fields' => [
        ['name' => 'email', 'type' => EmailType::class],
        ['name' => 'phone', 'type' => TelType::class],
    ]
    

Integration Tips

  • Validation Groups: Apply validation groups to fieldsets:
    $builder->add('billing', FieldsetType::class, [
        'validation_groups' => ['billing'],
    ]);
    
  • CSRF Protection: Fieldsets inherit CSRF protection from the parent form. Disable if embedding in non-CSRF contexts (e.g., APIs).
  • Theming: Override Twig templates for fieldsets (located in AdamQuaileFieldsetBundle:Form:fieldset.html.twig).

Gotchas and Tips

Pitfalls

  1. Label Conflicts: Set 'label' => false to avoid duplicate labels when using legend. The bundle doesn’t automatically suppress parent labels.

  2. CSRF Mismatches: Nested fieldsets in AJAX-loaded forms may trigger CSRF errors. Use 'csrf_protection' => false sparingly (only for non-submittable fieldsets).

  3. Deprecated Symfony: The bundle was last updated in 2017 for Symfony 2/3. Test thoroughly in newer versions (e.g., Symfony 5+ may require adjustments for form theme changes).

  4. Field Naming Collisions: Ensure field names (e.g., first_name) are unique across nested fieldsets to avoid array key conflicts in submitted data.

Debugging

  • Form Dumping: Use Symfony’s form dumper to inspect fieldset structure:
    $form->createView();
    dump($form->createView()->children);
    
  • Validation Errors: Fieldset-specific errors may not appear in the root form. Check nested errors property:
    $form->get('fieldset_name')->getErrors();
    

Extension Points

  1. Custom Fieldset Types: Extend FieldsetType to add logic (e.g., auto-populating fields):

    class CustomFieldsetType extends FieldsetType
    {
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults([
                'custom_option' => true,
            ]);
        }
    }
    
  2. Event Subscribers: Listen to PRE_SET_DATA or PRE_SUBMIT events to modify fieldsets dynamically:

    $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
        $data = $event->getData();
        if ($data->isPremium()) {
            $event->getForm()->add('premium_fields', FieldsetType::class);
        }
    });
    
  3. Twig Extensions: Create custom Twig filters to render fieldsets conditionally:

    {% if user.can_edit %}
        {{ form_row(form.address) }}
    {% endif %}
    

Configuration Quirks

  • Legacy Symfony: The bundle assumes Symfony’s legacy form theme system. For Symfony 4+, ensure twig:form_themes in config/packages/twig.yaml includes:
    form_themes: ['AdamQuaileFieldsetBundle:Form:fieldset.html.twig']
    
  • Translation: Fieldset legends/labels must be translatable. Use Symfony’s translation system:
    'legend' => 'address.details', // Key for translation
    
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui