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 Form Bundle Laravel Package

derafu/symfony-form-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the Bundle Add via Composer:

    composer require derafu/symfony-form-bundle
    

    Enable in config/bundles.php:

    Derafu\FormBundle\DerafuFormBundle::class => ['all' => true],
    
  2. Basic Form Integration Register the form type in a controller or service:

    use Derafu\FormBundle\Form\Type\DerafuFormType;
    
    $builder->add('myForm', DerafuFormType::class, [
        'config' => [
            'schema' => 'path/to/schema.json', // JSON schema for form structure
            'ui' => 'path/to/ui.json',       // UI schema for customization
        ],
    ]);
    
  3. First Use Case: Dynamic Forms Use the bundle to render a form from a JSON schema (e.g., for admin panels or user profiles):

    {{ form_start(form.myForm) }}
        {{ form_widget(form.myForm) }}
    {{ form_end(form.myForm) }}
    

Implementation Patterns

Workflow: Schema-Driven Forms

  1. Define Schemas Store form structure in schema.json (e.g., fields, validation rules) and UI customization in ui.json (e.g., labels, placeholders). Example schema.json:

    {
      "type": "object",
      "properties": {
        "name": { "type": "string", "title": "Full Name" },
        "email": { "type": "string", "format": "email" }
      },
      "required": ["name", "email"]
    }
    
  2. Integrate with Symfony Forms Bind schemas dynamically in controllers or services:

    $form = $this->createForm(DerafuFormType::class, $user, [
        'config' => [
            'schema' => $this->getParameter('kernel.project_dir').'/config/forms/user_schema.json',
        ],
    ]);
    
  3. Handle Submissions Process data like a standard Symfony form:

    if ($form->isSubmitted() && $form->isValid()) {
        $data = $form->getData();
        // Save $data to database
    }
    

Advanced Patterns

  • Dynamic Field Loading: Fetch schemas from a database or API and pass them to the form type.
  • Reusable Components: Create base schemas for common forms (e.g., login, contact) and extend them.
  • Event Listeners: Extend form behavior with Symfony events (e.g., PRE_SET_DATA, POST_SUBMIT).

Gotchas and Tips

Pitfalls

  1. Schema Validation

    • Ensure schema.json is valid JSON Schema (use JSON Schema Validator).
    • Symfony’s form validation runs after Derafu’s schema validation, which may lead to duplicate errors.
  2. UI Schema Conflicts

    • Overriding Symfony’s default form rendering (e.g., {{ form_widget }}) requires explicit template overrides.
    • UI schema paths must be resolvable (e.g., use @DerafuFormBundle/Resources/views/ for bundle assets).
  3. Dependency Injection

    • The bundle assumes derafu/form is installed. Add it explicitly if missing:
      composer require derafu/form
      

Debugging Tips

  • Enable Debug Mode: Set DERAFU_FORM_DEBUG=true in .env to log schema/UI parsing errors.
  • Check Compiled Assets: If UI customizations fail, verify Webpack Encore (or similar) is processing assets.
  • Symfony Profiler: Use the profiler to inspect form data and validation errors.

Extension Points

  1. Custom Form Types Extend DerafuFormType to add logic:

    class CustomDerafuFormType extends DerafuFormType {
        public function configureOptions(OptionsResolver $resolver) {
            $resolver->setDefaults([
                'custom_option' => true,
            ]);
        }
    }
    
  2. Override Templates Copy templates from vendor/derafu/symfony-form-bundle/Resources/views/ to templates/bundles/derafu_form/ to customize rendering.

  3. Event Subscribers Listen to derafu.form.pre_build or derafu.form.post_build events to modify forms dynamically:

    $eventDispatcher->addListener(DerafuFormEvents::PRE_BUILD, function (FormEvent $event) {
        $event->setForm($event->getForm()->add('extra_field', TextType::class));
    });
    
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.
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
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager