Bundle for BDF Form
composer require b2pweb/bdf-form-bundle
And then add to config/bundles.php :
<?php
return [
// ...
Bdf\Form\Bundle\FormBundle::class => ['all' => true],
];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: trueSimply 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');
}
public function withArgumentResolver(#[SubmitForm(validate: false)] MyForm $form)
{
// You can also use symfony argument resolver to automatically inject the form and submit it
// If you want the form to be validated automatically, you can set the `validate` parameter to its default value
// In this case, a InvalidFormException will be thrown if the form is invalid
if (!$form->valid()) {
throw new FormError($form->error());
}
$this->service->save($form->value());
return new Reponse('ok');
}
public function withArgumentResolverValue(#[SubmitForm(form: MyForm::class)] MyDto $value)
{
// The argument resolver can also submit, validate and generate the value automatically
$this->service->save($value);
return new Reponse('ok');
}
}This bundle supports BDF Form attribute.
Install the library using composer :
composer require b2pweb/bdf-form-attribute
Add configuration into config/packages/form.yaml
form:
attributes:
compile: true # enable compilation of attributes to native PHP code
configuratorClassPrefix: 'GeneratedForm\' # Define base namespace (or class prefix) for generated classes
configuratorClassSuffix: 'Configurator' # Define generated classes suffix
configuratorBasePath: '%kernel.build_dir%/form' # Define the save pathTo disable code generation during development, set configuration config/packages/dev/form.yaml :
form:
attributes:
compile: false # disable compilation to build dynamically formsOnce configured, you can simply declare forms like example.