metalogico/laravel-formello 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

metalogico/laravel-formello

Composer 安装命令:

composer require metalogico/laravel-formello

包简介

A Laravel package for generating Bootstrap 5 and Tailwind CSS 4 forms based on models

README 文档

README

image

Formello

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.

SCR-20250730-kida

🎉 Motivation

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 ⭐.

✨ Features

  • Fluent field builder with FormelloField::make() API
  • Reactive system for field interdependencies (client-side JS and/or server-side PHP callbacks)
  • Automatic form rendering with Bootstrap 5 and Tailwind CSS 4
  • Rich set of built-in widgets:
    • Text, Textarea, Hidden
    • Select (with multiple), TomSelect (AJAX, search, dependent selects)
    • Radio, Checkboxes, Toggle
    • Range
    • Date, DateTime (Flatpickr with Italian localization)
    • Mask (IMask.js input masking)
    • Color, ColorSwatch (Pickr nano)
    • Wysiwyg (Jodit Editor)
    • Upload
    • Separator
  • Customizable and extensible widgets
  • Modular asset management (disable libraries your theme already includes)
  • Automatic error handling and display
  • Artisan scaffolding command

⚠️ Upgrading to v2.x

Versions 2.0 and 2.1 introduce breaking changes from the 1.x series.

Breaking changes in v2.0

  • Fluent field builder: fields() now returns FormelloField[] instead of associative arrays
  • Select2 removed: use TomSelectWidget instead. HasSelect2Widget trait replaced by HasTomSelectWidget
  • jQuery removed: no longer a dependency
  • Widget rename: 'boolean' is now 'toggle'
  • Tailwind CSS 4: full widget support added

New in v2.1

  • Reactive System: callback-based field interdependencies with reactive.client (JS) and reactive.server (PHP)

Migration from v1.x

// 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 --force

🛠️ Installation

  1. Install the package via Composer:
composer require metalogico/laravel-formello
  1. Publish the assets:
php artisan vendor:publish --tag=formello-assets
  1. (Optional) Auto-publish assets on update

To 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"
    ]
}

😎 How to use

Creating a Form

Create a new form class that extends Metalogico\Formello\Formello. Fields are defined using the fluent FormelloField builder.

<?php

namespace App\Forms;

use App\Models\Category;
use Metalogico\Formello\Formello;
use Metalogico\Formello\FormelloField;

class ProductForm extends Formello
{
    protected function create(): array
    {
        return [
            'method' => 'POST',
            'action' => route('products.store'),
        ];
    }

    protected function edit(): array
    {
        return [
            'method' => 'PATCH',
            'action' => route('products.update', $this->model->id),
        ];
    }

    protected function fields(): array
    {
        return [
            FormelloField::make('name')
                ->label(__('Product Name'))
                ->help('Enter the name of the product')
                ->required(),

            FormelloField::make('description')
                ->label(__('Description'))
                ->widget('textarea'),

            FormelloField::make('category_id')
                ->label(__('Category'))
                ->widget('select')
                ->choices(Category::pluck('name', 'id')->toArray()),

            FormelloField::make('price')
                ->label(__('Price'))
                ->widget('mask', ['mask' => 'Number', 'scale' => 2])
                ->columns(6),

            FormelloField::make('in_stock')
                ->label(__('In Stock'))
                ->widget('toggle')
                ->columns(6),
        ];
    }
}

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',
    ];
}

Using the artisan command

You can generate a basic formello file using this command:

php artisan make:formello --model=Product

The script will generate a skeleton file that contains a basic field definition for each fillable field found in your model.

Rendering the Form

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 <form> tag) you can use:

@foreach ($formello->getFields() as $name => $field)
    {!! $formello->renderField($name) !!}
@endforeach

Conditional Logic

You 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,
    ];
}

CSS Framework Override

By default, Formello uses the framework set in config/formello.php. You can override it per form:

$formello = new ProductForm($product);
$formello->setCssFramework('tailwindcss4');

⚡ Reactive System

The reactive system enables field interdependencies without writing custom JavaScript from scratch. Add ->reactive() to any field to declare callbacks.

Client-side (instant)

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', '');
    },
};

Server-side (async PHP)

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 <meta name="csrf-token" content="{{ csrf_token() }}"> tag in your layout.

For full documentation see docs/reactive.md.

Creating Custom Widgets

Formello is designed to be extensible. To create a custom widget:

1. Publish the config file

php artisan vendor:publish --tag=formello-config

Add your widget alias to the custom_widgets array:

'custom_widgets' => [
    'star-rating' => App\Widgets\StarRatingWidget::class,
],

2. Create the Widget Class

Create a class extending Metalogico\Formello\Widgets\BaseWidget:

<?php

namespace App\Widgets;

use Metalogico\Formello\Widgets\BaseWidget;

class StarRatingWidget extends BaseWidget
{
    public function getViewData(string $name, $value, array $config, array $errors): array
    {
        return [
            'name' => $name,
            'value' => $value,
            'config' => $config,
            'errors' => $errors,
        ];
    }

    public function getTemplate(): string
    {
        return 'widgets.star-rating';
    }
}

3. Create the Blade Template

Create resources/views/widgets/star-rating.blade.php:

<div class="mb-3">
    <label for="{{ $name }}" class="form-label">{{ $config['label'] }}</label>
    <input type="number"
           name="{{ $name }}"
           id="{{ $name }}"
           value="{{ $value }}"
           class="form-control @if ($errors) is-invalid @endif"
           min="1"
           max="5" />

    @if ($errors)
        <div class="invalid-feedback">
            @foreach ($errors as $error)
                <div>{{ $error }}</div>
            @endforeach
        </div>
    @endif
</div>

4. Use it in your form

FormelloField::make('rating')
    ->label(__('Product Rating'))
    ->widget('star-rating'),

Custom widgets get the reactive system for free — just add ->reactive() to the field config.

🎨 Asset Management

Formello uses a modular system for loading JS and CSS assets, avoiding conflicts with themes that already include the same libraries.

Blade Directives

Add these directives to your layout:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Your App</title>

    <!-- Your existing CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- Formello CSS - loads only what's needed -->
    @formelloStyles
</head>
<body>
    <!-- Your content -->

    <!-- Your existing JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

    <!-- Formello JS - loads only what's needed -->
    @formelloScripts
</body>
</html>

Disabling Libraries

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,
],

Supported Libraries

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

📚 Documentation

⚖️ License

Laravel Formello is open-sourced software licensed under the MIT license.

🍺 Donations

If you really like this project and you want to help me please consider buying me a beer 🍺.

metalogico/laravel-formello 适用场景与选型建议

metalogico/laravel-formello 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 3.49k 次下载、GitHub Stars 达 101, 最近一次更新时间为 2025 年 07 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「bootstrap」 「Forms」 「laravel」 「form-builder」 「metalogico」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 metalogico/laravel-formello 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 metalogico/laravel-formello 我们能提供哪些服务?
定制开发 / 二次开发

基于 metalogico/laravel-formello 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.49k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 101
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 101
  • Watchers: 1
  • Forks: 4
  • 开发语言: JavaScript

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-21