一个 Laravel 包,用于基于模型生成 Bootstrap 5 和 TailwindCSS v4 表单。Laravel 11+
一个 Laravel 包,用于基于模型生成 Bootstrap 5 和 TailwindCSS v4 表单。Laravel 11+
A Laravel package for generating Bootstrap 5 and Tailwind CSS 4 forms based on models. Laravel 11+
Formello is a comprehensive form generation and handling tool for Laravel applications, inspired by Django forms.
The Laravel ecosystem offers powerful tools for building applications, from full-featured admin panels like Nova and Filament to complex form-handling libraries. However, I felt there was a need for a tool that sits in the "sweet spot" between these solutions.
Formello was created for developers who need to generate forms quickly without the overhead of a complete admin panel, but who also want a simpler, more intuitive API than more complex form libraries. It's designed to automate the repetitive aspects of form creation while giving you full control over the final output.
Currently, Formello ships with built-in support for Bootstrap 5 and Tailwind CSS 4.
If you use this project, please consider giving it a ⭐.
FormelloField::make() APIVersions 2.0 and 2.1 introduce breaking changes from the 1.x series.
fields() now returns FormelloField[] instead of associative arraysTomSelectWidget instead. HasSelect2Widget trait replaced by HasTomSelectWidget'boolean' is now 'toggle'reactive.client (JS) and reactive.server (PHP)// Before (v1.x)
protected function fields(): array
{
return [
'name' => [
'label' => 'Name',
'widget' => 'text',
],
];
}
// After (v2.x)
use Metalogico\Formello\FormelloField;
protected function fields(): array
{
return [
FormelloField::make('name')->label('Name')->widget('text'),
];
}After upgrading, republish the assets:
php artisan vendor:publish --tag=formello-assets --force
php artisan vendor:publish --tag=formello-config --forcecomposer require metalogico/laravel-formellophp artisan vendor:publish --tag=formello-assetsTo ensure that Formello's assets are automatically updated every time you run composer update, you can add a command to the post-update-cmd script in your project's composer.json file.
"scripts": {
"post-update-cmd": [
"@php artisan vendor:publish --tag=formello-assets --force"
]
}Create a new form class that extends Metalogico\Formello\Formello. Fields are defined using the fluent FormelloField builder.
…Remember to add these fields to your model's $fillable array otherwise Formello will not render them.
class Product extends Model
{
protected $fillable = [
'name',
'category_id',
'description',
'price',
'in_stock',
];
}You can generate a basic formello file using this command:
php artisan make:formello --model=ProductThe script will generate a skeleton file that contains a basic field definition for each fillable field found in your model.
In your controller for an empty form (create action):
public function create()
{
$formello = new ProductForm(Product::class);
return view('products.create', [
'formello' => $formello,
]);
}or, for an edit form:
public function edit(Product $product)
{
$formello = new ProductForm($product);
return view('products.edit', [
'formello' => $formello,
]);
}Then in your blade template:
{!! $formello->render() !!}If you want to render only the fields (without the `` tag) you can use:
@foreach ($formello->getFields() as $name => $field)
{!! $formello->renderField($name) !!}
@endforeachYou can use isCreating() and isEditing() methods in your form class to dynamically change fields based on the form's mode:
protected function fields(): array
{
$password_field = FormelloField::make('password')
->label(__('Password'))
->type('password');
if ($this->isCreating()) {
$password_field->required();
}
if ($this->isEditing()) {
$password_field->help('Leave empty to keep the current password');
}
return [
FormelloField::make('name')
->label(__('User Name'))
->help('Enter the name of the user'),
$password_field,
];
}By default, Formello uses the framework set in config/formello.php. You can override it per form:
$formello = new ProductForm($product);
$formello->setCssFramework('tailwindcss4');The reactive system enables field interdependencies without writing custom JavaScript from scratch. Add ->reactive() to any field to declare callbacks.
protected function fields(): array
{
return [
FormelloField::make('status')
->widget('select')
->choices(['active' => 'Active', 'other' => 'Other...'])
->reactive(['client' => 'onStatusChanged']),
FormelloField::make('status_other')
->label('Specify'),
];
}window.FormelloReactive = {
onStatusChanged(state) {
const isOther = state.get('status') === 'other';
state.setAttributes('status_other', {
hidden: !isOther,
required: isOther,
});
if (!isOther) state.set('status_other', '');
},
};For operations that need database access (e.g., dependent selects):
FormelloField::make('region_id')
->widget('tomselect')
->reactive(['server' => 'onRegionChanged']),
FormelloField::make('province_id')
->widget('tomselect'),use Metalogico\Formello\Support\FormelloState;
public function onRegionChanged(FormelloState $state): void
{
$region_id = $state->get('region_id');
$provinces = Province::where('region_id', $region_id)
->pluck('name', 'id')->toArray();
$state->setOptions('province_id', $provinces);
$state->set('province_id', null);
}Note: Server-side reactive requires a `` tag in your layout.
For full documentation see docs/reactive.md.
Formello is designed to be extensible. To create a custom widget:
php artisan vendor:publish --tag=formello-configAdd your widget alias to the custom_widgets array:
'custom_widgets' => [
'star-rating' => App\Widgets\StarRatingWidget::class,
],Create a class extending Metalogico\Formello\Widgets\BaseWidget:
$name,
'value' => $value,
'config' => $config,
'errors' => $errors,
];
}
public function getTemplate(): string
{
return 'widgets.star-rating';
}
}Create resources/views/widgets/star-rating.blade.php:
@endforeach
@endif
FormelloField::make('rating')
->label(__('Product Rating'))
->widget('star-rating'),Custom widgets get the reactive system for free — just add ->reactive() to the field config.
Formello uses a modular system for loading JS and CSS assets, avoiding conflicts with themes that already include the same libraries.
Add these directives to your layout:
Your App
@formelloStyles
@formelloScripts
If your theme already includes a library, disable it in config/formello.php:
'assets' => [
'tomselect' => false, // theme already has Tom Select
'date' => true,
'datetime' => true,
'mask' => true,
'color' => true,
'colorswatch' => true,
'wysiwyg' => true,
],| Widget | Library | Assets |
|---|---|---|
| TomSelect | Tom Select | tom-select.complete.js, tom-select.default.min.css |
| Date, DateTime | Flatpickr | flatpickr.min.js, l10n/it.js, flatpickr.min.css |
| Mask | IMask | imask.min.js |
| Color, ColorSwatch | Pickr | pickr.min.js, nano.min.css |
| Wysiwyg | Jodit | jodit.min.js, jodit.min.css |
Laravel Formello is open-sourced software licensed under the MIT license.
If you really like this project and you want to help me please consider buying me a beer .
暂无开放 Issues,或尚未同步最近议题。