formfy/easy-laravel-form
Laravel form generator for building and rendering forms quickly. Define fields via a DBFormBuilder class, bind models, handle validation errors from the session, customize field options (text/select), and set submit labels, then render in Blade with minimal code.
Installation
composer require formfy/easy-laravel-form
Basic Form Setup
Extend DBFormBuilder and define fields in a method:
use Kian\EasyLaravelForm\DBFormBuilder;
class StudentForm extends DBFormBuilder {
public function __construct() {
parent::__construct('', null, 'POST');
$this->studentForm();
}
public function studentForm() {
$this->addField('text', 'firstname', 'Firstname');
}
}
Render the Form
$form = new StudentForm();
echo $form->render();
First Use Case
Quickly scaffold a CRUD form for a Student model:
$student = Student::find(1);
$form = new StudentForm('', $student);
echo $form->render();
Model Binding Pass an Eloquent model to auto-populate fields:
$form = new StudentForm('', Student::find(1));
Field Customization Use chained methods for field attributes:
$this->addField('text', 'email', 'Email')
->required()
->rules(['email'])
->placeholder('user@example.com');
Dynamic Form Logic Conditionally add fields based on model state:
if ($this->model->isAdmin()) {
$this->addField('checkbox', 'admin', 'Admin Access');
}
Validation Integration Leverage Laravel’s validation rules:
$this->addField('text', 'phone', 'Phone')
->rules(['required', 'digits:10']);
Form Submission Handling
Use the built-in handleSubmit() method:
if ($form->handleSubmit()) {
$form->save(); // Auto-saves bound model
}
Blade Integration
@php
$form = new StudentForm();
@endphp
{!! $form->render() !!}
API Responses
Use form->getErrors() to return validation errors in JSON:
return response()->json(['errors' => $form->getErrors()]);
Custom Field Types Extend the builder for reusable components:
$this->addField('custom', 'profile_picture', 'Profile Picture')
->customView('custom.profile-picture');
Error Handling
$errors = session('errors') ?? [];
$form = new StudentForm('', null, 'POST', $errors);
form->getErrors() to access validation messages programmatically.Model Binding Quirks
$this->allowFields(['custom_field']); // Whitelist non-fillable fields
CSRF Protection
Ensure the form includes @csrf in Blade or manually:
$form->addCsrfField(); // Add CSRF token programmatically
Field Overrides
addField() calls overwrite earlier ones with the same name.Inspect Fields
Use dd($form->getFields()) to debug field configurations.
Validation Debugging Check raw validation errors:
dd($form->validator->errors()->messages());
Custom Field Types
Override addField() or create a decorator:
$this->addField('select', 'country', 'Country')
->options(['US' => 'United States', 'CA' => 'Canada']);
Form Events Hook into submission via traits or observers:
$form->onSubmit(function () {
// Pre-submission logic
});
Localization Extend field labels dynamically:
$this->addField('text', 'name', trans('form.name'));
Asset Management
Use addAsset() for JS/CSS:
$this->addAsset('css', 'formfy.css');
How can I help you explore Laravel packages today?