abdallahmohammed/laravel-multistep-forms

Laravel 多步骤表单构建器

v1.0.3 2021-01-08 16:30 UTC

This package is auto-updated.

Last update: 2024-09-09 00:56:36 UTC


README

Build Status

安装

composer require abdallahmohammed/laravel-multistep-forms

示例用法

use AbdallaMohammed\Form\Form;
use Illuminate\Support\Facades\Route;

Route::get('form', function () {
    return app(Form::class)->make(function (Form $form) {
        // Create a step instance and define rules, messages and attributes
        $form->step()->rules([
            'name' => ['required', 'string'],
        ])->messages([
            'required' => ':attribute Required',
        ])->attributes([
            'name' => 'Name',
        ]);

        // Add another step with dynamic rules
        $form->step()->dynamicRules();
    });
})->name('form');

步骤用法

步骤之前

定义一个回调,在验证步骤之前触发。

从这个钩子返回响应以在验证之前提前返回。

before($step, Closure $closure)

$step 可以是 Step 实例,或者步骤的编号。

步骤之后

定义一个回调,在验证步骤之后触发。步骤编号或 * 用于所有步骤。

从这个钩子返回响应以在表单步骤增加之前提前返回。

after($step, Closure $closure)

$step 可以是 Step 实例,或者步骤的编号。

动态步骤

您可以设置一个步骤为动态,这样该步骤将从请求中获取它的 规则消息属性

例如

use AbdallaMohammed\Form\Form;
use Illuminate\Support\Facades\Route;

Route::get('form', function () {
    return app(Form::class)->make(function (Form $form) {
        ...
        $form->step()->dynamicRules()->messages([
            'foo' => 'bar',
        ]);
        ...
    });
})->name('form');

从示例中,我们已定义了 属性 而没有 规则,因此我们必须在请求中发送规则。以下是请求体的示例。

{
  "step": 1,
  "1.rules": {
    "name": ["required", "string"]
  }
}

1.rules 是对第一个步骤规则的引用。

您可以将 1 更改为动态步骤的编号。

与前面的示例一样,您可以在请求体中发送 1.messages1.attributes

辅助方法

stepConfig(?int $step = null)

获取当前步骤配置,或特定步骤配置。

getValue(string $key, $fallback = null)

从表单状态(会话/旧输入)获取字段值或回退到默认值。

setValue(string $key, $value)

从会话表单状态设置字段值。

currentStep()

获取当前保存的步骤编号。

requestedStep()

获取请求的步骤编号。

isStep(int $step = 1)

获取当前步骤编号。

isLastStep()

确定当前步骤是否是最后一个步骤。

isPast(int $step, $truthy = true, $falsy = false)

确定指定的步骤是否在过去。

isActive(int $step, $truthy = true, $falsy = false)

确定指定的步骤是否处于活动状态。

isNext(int $step, $truthy = true, $falsy = false)

确定指定的步骤是否在下一次。

toCollection

获取表单状态的数组表示形式作为一个集合。

toArray

获取表单状态的数组表示形式。