Formello: A Practical Solution for Creating Forms in Laravel 🚀
If you frequently code in Laravel, you’ve probably experienced the hassle of creating repetitive forms. From creating input fields, validation, error handling, to rendering in Blade. Well, now there’s Formello, a Laravel package specifically designed to simplify developers’ lives when creating forms.
Formello offers the perfect compromise:
Simpler than complex form libraries.
Lighter than full-featured admin panels such as Nova or Filament.
So, if you need to create forms quickly but still want them to be flexible and easy to manage, Formello could be your new best friend.
Key Features of Formello
Easy-to-use form definition using Laravel classes.
Automatic rendering directly to Blade.
Supports various fields: text, textarea, select, select2, radio, checkbox, toggle, date, datetime, upload, hidden, and custom widgets.
Automatic error handling (no need to create manually).
Form validation is directly integrated.
Styling support: Bootstrap 5 (Tailwind CSS coming soon).
With this feature, you can focus on the logic of the application without having to copy and paste boilerplate forms.
Formello Installation
Installation is very easy. Install the package via composer:
composer require metalogico/laravel-formello
Publish your assets:
php artisan vendor:publish --tag=formello-assets
To automatically update assets every time composer updates, just add this to composer.json:
"scripts": {
"post-update-cmd": [
"@php artisan vendor:publish --tag=formello-assets --force"
]
}
How to Use Formello in Laravel
Alright, let’s get hands-on with Formello. Here’s how you can start using it in your Laravel project.
1. Create a Form Class
First, create a form class that extends Metalogico\Formello\Formello.
Here’s a simple example for a ProductForm:
<?php
namespace App\Forms;
use Metalogico\Formello\Formello;
use Metalogico\Formello\Widgets\SelectWidget;
class ProductForm extends Formello
{
protected function create(): array
{
return [
'method' => 'POST',
'action' => route('products.store'),
];
}
protected function edit(): array
{
return [
'method' => 'POST',
'action' => route('products.update', $this->model->id),
];
}
protected function fields(): array
{
return [
'name' => [
'label' => __('Product Name'),
'help' => 'Enter the name of the product',
],
'description' => [
'label' => __('Description'),
],
'category_id' => [
'label' => __('Category'),
'widget' => 'select',
'choices' => Category::pluck('name', 'id')->toArray(),
],
'in_stock' => [
'label' => __('In Stock'),
],
];
}
}
⚠️ Note: Make sure these fields are added to your model’s $fillable array, otherwise Formello won’t render them.
class Product extends Model
{
protected $fillable = [
'name',
'category_id',
'description',
'in_stock',
];
}
👉 Read the full article at ramacan.dev
Top comments (0)