Installation:
composer require b2pweb/bdf-form
Register the service provider in config/app.php under providers:
B2PWeb\BDFForm\BDFFormServiceProvider::class,
Basic Usage:
Create a form class extending B2PWeb\BDFForm\BDFForm:
use B2PWeb\BDFForm\BDFForm;
class MyForm extends BDFForm
{
protected $fields = [
'name' => ['type' => 'text', 'label' => 'Your Name'],
'email' => ['type' => 'email', 'label' => 'Email Address'],
];
}
Rendering:
Use the render() method in a Blade view:
$form = new MyForm();
echo $form->render();
Handling Submissions:
$form = new MyForm();
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
// Process data (e.g., save to DB)
}
Build a contact form with validation:
class ContactForm extends B2PWeb\BDFForm\BDFForm
{
protected $fields = [
'name' => ['type' => 'text', 'rules' => 'required|min:3'],
'email' => ['type' => 'email', 'rules' => 'required|email'],
'message' => ['type' => 'textarea', 'rules' => 'required|min:10'],
];
protected $submitButton = 'Send Message';
}
Form Creation:
BDFForm and define $fields as an associative array.text, email, textarea, select, checkbox, etc.) or custom types via type key.Validation:
required, min, email) via the rules key.validate() for custom logic:
protected function validate(array $data): array
{
$rules = parent::validate($data);
$rules['email'][] = 'unique:users'; // Example custom rule
return $rules;
}
Dynamic Fields:
addField() or removeField() dynamically:
$form->addField('age', ['type' => 'number', 'label' => 'Age']);
Integration with Laravel:
public function showForm(MyForm $form) { ... }
if (!$form->isValid()) {
return back()->withErrors($form->errors())->withInput();
}
CSRF Protection:
Enable via config (config/bdf-form.php):
'csrf' => env('APP_DEBUG') ? false : true,
Blade Macros: Create reusable form components:
Blade::component('form', function ($form) {
return $form->render();
});
Usage:
@form($form)
Localization: Use Laravel’s localization features for labels/errors:
'name' => ['type' => 'text', 'label' => __('form.name')],
Assets: Override default assets (CSS/JS) via config:
'assets' => [
'css' => 'path/to/custom.css',
'js' => 'path/to/custom.js',
],
CSRF Mismatch:
csrf is enabled in config but the form lacks a CSRF token, submissions will fail.{{ csrf_field() }} is included in Blade or manually add the token:
$form->addHidden('_token', csrf_token());
Validation Rules:
unique require database access. If the connection fails, validation silently passes.Field Naming Collisions:
addField('email', [...]) twice) may overwrite data.Asset Loading:
config/bdf-form.php and check storage/framework/views for cached issues.Enable Debug Mode:
Set 'debug' => true in config/bdf-form.php to log errors and validation failures to Laravel’s log.
Inspect Form Data:
Use dd($form->getData()) or dd($form->errors()) to debug submissions.
Check Field Types:
Typos in field types (e.g., 'type' => 'textbox') will render as plain text. Use valid types from the documentation.
Custom Field Types:
Create a new class extending B2PWeb\BDFForm\Field\AbstractField:
class CustomField extends AbstractField
{
public function render(): string
{
return '<input type="custom" name="' . $this->name . '">';
}
}
Register it in config/bdf-form.php:
'field_types' => [
'custom' => \App\CustomField::class,
],
Override Rendering:
Extend the render() method in your form class:
public function render(): string
{
return '<div class="custom-form">' . parent::render() . '</div>';
}
Event Hooks:
Listen for form events (e.g., form.submitted) via Laravel’s event system:
event(new \B2PWeb\BDFForm\Events\FormSubmitted($form));
Middleware: Use middleware to pre-process forms:
public function handle($request, Closure $next)
{
$form = new MyForm();
$form->setData($request->all());
if ($form->isValid()) {
// Pre-process data
}
return $next($request);
}
How can I help you explore Laravel packages today?