PHP code example of b2pweb / bdf-form-bundle
1. Go to this page and download the library: Download b2pweb/bdf-form-bundle library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
b2pweb / bdf-form-bundle example snippets
return [
// ...
Bdf\Form\Bundle\FormBundle::class => ['all' => true],
];
// 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');
}
}