定制 codions/laravel-livewire-forms 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

codions/laravel-livewire-forms

最新稳定版本:v0.1.1

Composer 安装命令:

composer require codions/laravel-livewire-forms

包简介

Laravel Livewire form component with declarative Bootstrap 5 fields and buttons.

README 文档

README

Laravel Livewire form component with declarative Bootstrap 5 fields and buttons.

Installation

composer require codions/laravel-livewire-forms

Usage

Make a new form:

php artisan make:form users.create-user-form

Declare fields:

public function fields()
{
    return [
        Input::make('name', 'Name'),
        Input::make('email', 'Email')->type('email'),
        Input::make('password', 'Password')->type('password'),
    ];
}

Declare buttons:

public function buttons()
{
    return [
        Button::make('Create User')->click('createUser'),
        Button::make('Cancel', 'secondary')->url('/'),
    ];
}

Declare rules:

public function rules()
{
    return [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8'],
    ];
}

Declare an action (notice the use of ->click('createUser') in the button example above:

public function createUser()
{
    $this->validate();

    User::create([
        'name' => $this->data('name'),
        'email' => $this->data('email'),
        'password' => Hash::make($this->data('password')),
    ]);

    return redirect('/');
}

Form Slider

Create a form that slides out by specifying a title, layout and route to use:

class Login extends FormSliderComponent
{
    public $title = 'Login';
    public $layout = 'layouts.card';
    public $btnText = 'Login';
}

Setting Initial Data

You can set the initial form data / defaults via the data array property in your component mount method:

class UpdateUserForm extends FormComponent
{
    public $title = 'Update User';
    
    public function mount(User $user)
    {
        $this->data = $user->toArray();
    }
}

Accessing Data

Use the data method in the component to access current form data (you can use dot notation for array values):

public function createUser()
{
    User::create([
        'name' => $this->data('name'),
        'email' => $this->data('email'),
        'likes_turtles' => $this->data('answers.likes_turtles'),
    ]);
}

Data Binding

Most fields allow you to change the way livewire binds data via helper methods that are chained to fields e.g.:

Input::make('name', 'Name'), // defaults to defer
Input::make('name', 'Name')->instant(), // bind on keyup 
Input::make('name', 'Name')->defer(), // bind on action 
Input::make('name', 'Name')->lazy(), // bind on change
Input::make('name', 'Name')->debounce(500), // bind after 500ms delay 

Sizing

Many fields also allow you to specify a size for the input e.g.:

Input::make('name', 'Name'), // defaults to normal sizing
Input::make('name', 'Name')->small(), // small sizing
Input::make('name', 'Name')->large(), // large sizing

Disabling

Some fields allow you to disable or set them to readonly, and even plaintext for inputs:

Input::make('name', 'Name')->disabled(),
Input::make('name', 'Name')->readonly(),
Input::make('name', 'Name')->plaintext(),

Helper Text

Specify helper text for a field by using the help method:

Input::make('name', 'Name')->help('Please tell us your name!'),

Available Fields

Alert ($message, $style = 'success')

An alert box.

Alert::make('It worked!'),
Alert::make('Something bad happened.', 'danger'),
Alert::make('Something else happened.')->dismissible(),

The $style parameter accepts a bootstrap alert style e.g. success, danger, primary, etc. Use the dismissible method to make the alert dismissible.

Available methods: dismissible

Arrayable ($name, $label = null)

An array of fields.

Arrayable::make('locations', 'Locations')->fields([
    Input::make('city')->placeholder('City'),
    Select::make('state')->placeholder('State')->options(['FL', 'TX']),
]),

Available methods: fields, help, disabled

Bootstrap Grid

A bootstrap support to the form.

Row ($label = null) and RowColumn ($label = null)

An array of fields display in a Bootstrap Row or Column

Row::make()->fields([
    Input::make('city')->placeholder('City'),
    Select::make('state')->placeholder('State')->options(['FL', 'TX']),
]),

Available methods: fields, help, disabled, isColumn, col_class

Button ($label = 'Submit', $style = 'primary')

A button used for actions and links.

Button::make('Register')->click('register'),
Button::make('Already registered?', 'secondary')->route('login'),
Button::make('Go back home', 'link')->url('/'),

The $style parameter accepts a bootstrap button style e.g. primary, outline-secondary, link, etc. Use the block method to make a button full width.

Available methods: block, click, href, route, url

Checkbox ($name, $label)

A checkbox field.

Checkbox::make('accept', 'I accept the terms'),
Checkbox::make('accept', 'I accept')->help('Please accept our terms'),
Checkbox::make('active', 'This user is active')->switch(),

Use the switch method to style the checkbox as a switch.

Available methods: switch, help, instant, addAttrs, defer, lazy, debounce, disabled

Checkboxes ($name, $label = null)

An array of checkbox fields.

Checkboxes::make('colors', 'Colors')->options(['Red', 'Green', 'Blue']),

Available methods: options, switch, help, instant, addAttrs, defer, lazy, debounce, disabled

Color ($name, $label = null)

A color picker field.

Color::make('hair_color', 'Hair Color'),

Available methods: small, large, containerSize, help, instant, addAttrs, defer, lazy, debounce, disabled, readonly

Conditional

A statement used to conditionally show fields.

Conditional::if($this->data('color') == 'green', [
    Input::make('green', 'Green'),
])->elseif($this->data('color') == 'blue', [
    Input::make('blue', 'Blue'),
])->else([
    Input::make('red', 'Red'),
]),

Available methods: if, elseif, else

DynamicComponent ($name, $attrs = [])

A field used to display dynamic third-party components.

DynamicComponent::make('honey'),
DynamicComponent::make('honey', ['recaptcha' => true]),

This would translate to <x-honey/> and <x-honey recaptcha="recaptcha"/> in your form.

File ($name, $label = null)

A file upload field.

File::make('avatar', 'Avatar'),
File::make('photos', 'Photos')->multiple(),
File::make('documents', 'Documents')->multiple()->disk('s3'),

Use the multiple method to allow multiple file uploads. Optionally specify the filesystem disk to use via the disk method (used for linking to files, defaults to the filesystem config default).

Available methods: disk, multiple, help, disabled

Input ($name, $label = null)

An input field.

Input::make('name', 'Name'),
Input::make('phone')->placeholder('Phone')->type('tel'),
Input::make('email', 'Email')->type('email')->large(),
Input::make('price', 'Price')->type('number')->append('$')->prepend('.00'),

The type method accepts a standard HTML input type. As with other inputs, use small or large to resize an input. Input fields also support appends/prepends, and even plaintext.

Available methods: small, large, containerSize, help, instant, addAttrs, defer, lazy, debounce, disabled, readonly, placeholder, type, append, prepend, plaintext

Radio ($name, $label = null)

A radio field.

Radio::make('gender', 'Gender')->options(['Male', 'Female']),

Available methods: options, switch, help, instant, addAttrs, defer, lazy, debounce, disabled

Select ($name, $label = null)

A select dropdown field.

Select::make('color', 'Color')->options(['Red', 'Green', 'Blue']),
Select::make('color', 'Color')->options([
    '#ff0000' => 'Red',
    '#00ff00' => 'Green',
    '#0000ff' => 'Blue',
])->instant(),
Select::make('user_id', 'User')->options(User::pluck('name', 'id')->toArray()),

Available methods: options, small, large, containerSize, help, instant, addAttrs, defer, lazy, debounce, disabled, placeholder

Textarea ($name, $label = null)

A textarea field.

Input::Textarea('bio', 'Biography'),
Input::Textarea('bio', 'Biography')->rows(5),

Available methods: small, large, containerSize, help, instant, addAttrs, defer, lazy, debounce, disabled, readonly, placeholder, rows

View ($name, $data = [])

Used to render a custom Blade view inside the form.

View::make('custom-view', ['hello' => 'world']),

Sameple Example

Code

namespace App\Http\Livewire\Clients;

use Codions\LaravelLivewireForms\Components\Button;
use Codions\LaravelLivewireForms\Components\FormComponent;
use Codions\LaravelLivewireForms\Components\Input;
use Codions\LaravelLivewireForms\Components\Select;

class CreateClientForm extends FormComponent
{
    public $gridClass = 'row';

    public function fields()
    {
        return [
            Row::make()->fields([
                Input::make('name', 'Name')
                    ->placeholder('Full Name'),
                Input::make('email', 'Email')
                    ->type('email')
                    ->placeholder('Email, example: user@example.com'),
                Select::make('gender', 'Gender')
                    ->placeholder('Gender')
                    ->options(['Male', 'Female'])
                    ->addAttrs(['class' => 'd-block w-full']),
                Input::make('phone_no', 'Contact Number')
                    ->placeholder('(xxx) xxx xxxxx'),
                Input::make('street_address', 'Street Address'),
                Input::make('city', 'City'),
                Input::make('state', 'State / Parist'),
                Input::make('country', 'Country'),
            ])
        ];
    }

    public function buttons()
    {
        return [
            Button::make('Cancel', 'secondary')->url(route('team.index')),
            Button::make()->click('submit'),
        ];
    }
}

Credits

License

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

codions/laravel-livewire-forms 适用场景与选型建议

codions/laravel-livewire-forms 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 05 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-16