b2pweb/bdf-form-bundle

Bundle for BF Form

Installs: 137

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 0

Open Issues: 0

Type:symfony-bundle

dev-master / 1.0.x-dev 2021-10-25 12:02 UTC

This package is auto-updated.

Last update: 2021-11-25 12:07:08 UTC


README

Bundle for BDF Form

Build Status Packagist Version Total Downloads

Installation

composer require b2pweb/bdf-form-bundle

And then add to config/bundles.php :

<?php

return [
    // ...
    Bdf\Form\Bundle\FormBundle::class => ['all' => true],
];

Configuration

To enable auto registration of custom forms and builders, simply enable autoconfigure and load the package sources :

services:
    _defaults:
        autowire: true
        autoconfigure: true

    App\Form\:
        resource: './src/Form/*'

To use CSRF, do not forget to enable the CSRF service :

framework:
    csrf_protection:
        enabled: true

Usage

Simply use the container to instantiate the custom form.

Note: The container will automatically inject all dependencies

// Declare the form
class MyForm extends \Bdf\Form\Custom\CustomForm
{
    /**
     * @var MyService 
     */
    private $service;
    
    // You can declare dependencies on the constructor 
    public function __construct(MyService $service, ?\Bdf\Form\Aggregate\FormBuilderInterface $builder = null) 
    {
        parent::__construct($builder);
        
        $this->service = $service;
    }

    protected function configure(\Bdf\Form\Aggregate\FormBuilderInterface $builder) : void
    {
        // Configure fields
    }
}

// The controller
class MyController extends AbstractController
{
    public function save(Request $request)
    {
        // Create the form using the container
        $form = $this->container->get(MyForm::class);
        
        // Submit data
        if (!$form->submit($request->request->all())->valid()) {
            throw new FormError($form->error());
        }
        
        $this->service->save($form->value());
        
        return new Reponse('ok');
    }
}