gustiawan/laravel-form-builder

该软件包的最新版本(1.6.0)没有可用的许可信息。

Laravel 表单构建器

1.6.0 2023-09-21 03:00 UTC

This package is auto-updated.

Last update: 2024-09-21 05:12:45 UTC


README

安装

composer require gustiawan/laravel-form-builder

运行此命令以发布软件包

php artisan vendor:publish --provider="Gustiawan\FormBuilder\FormBuilderServiceProvider"

配置

如果你想使用 Bootstrap 风格,请更改样式为 bootstrap

    /**
     * Set your default styling 
     * the option is [tailwind, bootstrap]
     */
    "style" => "tailwind",

仅当你想使用自定义表单模板时更改此设置

    /**
     * Fill this with your view path
     */
    "form_template" => null

使用方法

使用

php artisan make:form {name}

文件位于 App/Form

根据需要添加你的表单

use Gustiawan\FormBuilder\Form;

class ExampleForm extends Form
{
    public function handle() 
    {
        $this->text("textinput", "Text Input");
        $this->password("password", "Old Password");
        $this->textArea("text area", "Text Area", "Default Value");
        $this->date("example_date", "Example Date");
        $this->radio("radio_example", "Radio Example", [1 => "Option one", 2 => "Option two"], ["value" => 1]); // default value
        $this->checkBox("checkbox_example", "Checkbox Example", [1 => "Option one", 2 => "Option two"], ["value" => [1, 2]]); // default value must be array
        $this->select("select_field", "Select Field Example", [1 => "Option one", 2 => "Option two"], ["value" => 1]); // default value
    }
}

然后在你的控制器中

public function example() 
{
    $form = new ExampleForm([
        "action" => route('example_routes'),
        "method" => "POST",
        // optional
        "data" => [
            "field_name" => "some value"
        ]
    ]);

    return view('example_view', compact('form'));
}

在你的视图中

<x-form-generator-form :form="$form" />

字段选项

为字段添加默认值

$this->text("textinput", "Text Input", ["value" => "some value"]);

使字段成为必填字段

$this->text("textinput", "Text Input", ["required" => true]);

使字段为只读

$this->text("textinput", "Text Input", ["readonly" => true]);

使字段为禁用状态

$this->text("textinput", "Text Input", ["disabled" => true]);

为输入添加自定义类

$this->text("textinput", "Text Input", ["class" => "datetimepicker"]);