2022-11-04 kris/laravel-form-builder 快速生成表单

官网地址
https://packagist.org/packages/kris/laravel-form-builder

官方文档

1、安装

1.1、Using Composer

composer require kris/laravel-form-builder

1.2、Or manually by modifying composer.json file:

{
    "require": {
        "kris/laravel-form-builder": "1.*"
    }
}

1.3、 run composer install

2、配置

Then add Service provider to config/app.php

    'providers' => [
        // ...
        Kris\LaravelFormBuilder\FormBuilderServiceProvider::class
    ]

And Facade (also in config/app.php)

    'aliases' => [
        // ...
        'FormBuilder' => Kris\LaravelFormBuilder\Facades\FormBuilder::class
    ]

Notice: This package will add laravelcollective/html package and load aliases (Form, Html) if they do not exist in the IoC container.
注:如果IoC容器中不存在别名(Form、html),此包将添加laravelcollective/html包并加载别名。

3、Quick start(快速启动)

3.1、Creating form classes is easy. With a simple artisan command:
创建表单类很容易。通过简单的artisan命令:

php artisan make:form Forms/SongForm --fields="name:text, lyrics:textarea, publish:checkbox"

3.2、Form is created in path app/Forms/SongForm.php with content:
表单在路径 app/Forms/ 中创建SongForm.php,内容:

add('name', 'text')
            ->add('lyrics', 'textarea')
            ->add('publish', 'checkbox');
    }
}

3.3、If you want to instantiate empty form without any fields, just skip passing --fields parameter:
如果要实例化没有任何字段的空表单,只需跳过传递--fields参数:

php artisan make:form Forms/PostForm

Gives:

3.4、After that instantiate the class in the controller and pass it to view:
之后,在控制器中实例化类并将其传递给视图:

create(\App\Forms\SongForm::class, [
            'method' => 'POST',
            'url' => route('song.store')
        ]);

        return view('song.create', compact('form'));
    }

    public function store(FormBuilder $formBuilder)
    {
        $form = $formBuilder->create(\App\Forms\SongForm::class);

        if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }

        // Do saving and other things...
    }
}

或者

public function create(FormBuilder $formBuilder){

    $form = $formBuilder->create(\App\Form\SongForm::class,[

       'method' => 'POST',

        'url' => route('song.store')

    ]);

    return view('song.form',compact('form'));

}

视图:







  

  





  {!! form($form) !!}




你可能感兴趣的:(2022-11-04 kris/laravel-form-builder 快速生成表单)