Laravel 5 的表单构建器,灵感来自 Symfony 的表单构建器。在 Laravels FormBuilder 类的帮助下,创建可以轻松修改和重用的表单。默认情况下它支持 Bootstrap 3。
对于 Laravel 4 版本,请检查 laravel4-form-builder。
要使用 bootstrap 4 而不是 bootstrap 3,请安装 laravel-form-builder-bs4。
如果您从1.5.*
>1.6.*
*,并且在表单值绑定方面遇到问题,请将default_value
重命名为value
。
更多信息请参见变更日志。
有关详细文档,请参阅 https://kristijanhusak.github.io/laravel-form-builder/。
可以在此处找到更改日志。
composer require kris/laravel-form-builder
或者通过修改composer.json
文件手动:
{
"require" : {
"kris/laravel-form-builder" : " 1.* "
}
}
并运行composer install
然后将服务提供者添加到config/app.php
' providers ' => [
// ...
Kris LaravelFormBuilder FormBuilderServiceProvider::class
]
和 Facade (也在config/app.php
中)
' aliases ' => [
// ...
' FormBuilder ' => Kris LaravelFormBuilder Facades FormBuilder::class
]
注意:如果 IoC 容器中不存在,此包将添加laravelcollective/html
包并加载别名(Form、Html)。
创建表单类很容易。使用简单的 artisan 命令:
php artisan make:form Forms/SongForm --fields= " name:text, lyrics:textarea, publish:checkbox "
表单在路径app/Forms/SongForm.php
中创建,内容为:
<?php
namespace App Forms ;
use Kris LaravelFormBuilder Form ;
use Kris LaravelFormBuilder Field ;
class SongForm extends Form
{
public function buildForm ()
{
$ this
-> add ( ' name ' , Field:: TEXT , [
' rules ' => ' required|min:5 '
])
-> add ( ' lyrics ' , Field:: TEXTAREA , [
' rules ' => ' max:5000 '
])
-> add ( ' publish ' , Field:: CHECKBOX );
}
}
如果你想实例化没有任何字段的空表单,只需跳过传递--fields
参数:
php artisan make:form Forms/PostForm
给出:
<?php
namespace App Forms ;
use Kris LaravelFormBuilder Form ;
class PostForm extends Form
{
public function buildForm ()
{
// Add fields here...
}
}
之后在控制器中实例化该类并将其传递给视图:
<?php
namespace App Http Controllers ;
use Illuminate Routing Controller as BaseController ;
use Kris LaravelFormBuilder FormBuilder ;
class SongsController extends BaseController {
public function create ( FormBuilder $ formBuilder )
{
$ form = $ formBuilder -> 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...
}
}
替代示例:
<?php
namespace App Http Controllers ;
use Illuminate Routing Controller as BaseController ;
use Kris LaravelFormBuilder FormBuilder ;
use App Forms SongForm ;
class SongsController extends BaseController {
public function create ( FormBuilder $ formBuilder )
{
$ form = $ formBuilder -> create (SongForm::class, [
' method ' => ' POST ' ,
' url ' => route ( ' song.store ' )
]);
return view ( ' song.create ' , compact ( ' form ' ));
}
public function store ( FormBuilder $ formBuilder )
{
$ form = $ formBuilder -> create (SongForm::class);
if (! $ form -> isValid ()) {
return redirect ()-> back ()-> withErrors ( $ form -> getErrors ())-> withInput ();
}
// Do saving and other things...
}
}
如果您想在表单提交后存储模型,并考虑到所有字段都是模型属性:
<?php
namespace App Http Controllers ;
use App Http Controllers Controller ;
use Kris LaravelFormBuilder FormBuilder ;
use App SongForm ;
class SongFormController extends Controller
{
public function store ( FormBuilder $ formBuilder )
{
$ form = $ formBuilder -> create ( App Forms SongForm::class);
$ form -> redirectIfNotValid ();
SongForm:: create ( $ form -> getFieldValues ());
// Do redirecting...
}
您只能保存您需要的属性:
<?php
namespace App Http Controllers ;
use App Http Controllers Controller ;
use Kris LaravelFormBuilder FormBuilder ;
use App SongForm ;
class SongFormController extends Controller
{
public function store ( FormBuilder $ formBuilder , Request $ request )
{
$ form = $ formBuilder -> create ( App Forms SongForm::class);
$ form -> redirectIfNotValid ();
$ songForm = new SongForm ();
$ songForm -> fill ( $ request -> only ([ ' name ' , ' artist ' ])-> save ();
// Do redirecting...
}
或者您可以在表单提交后更新任何模型:
<?php
namespace App Http Controllers ;
use App Http Controllers Controller ;
use Kris LaravelFormBuilder FormBuilder ;
use App SongForm ;
class SongFormController extends Controller
{
public function update ( int $ id , Request $ request )
{
$ songForm = SongForm:: findOrFail ( $ id );
$ form = $ this -> getForm ( $ songForm );
$ form -> redirectIfNotValid ();
$ songForm -> update ( $ form -> getFieldValues ());
// Do redirecting...
}
创建路线
// app/Http/routes.php
Route:: get ( ' songs/create ' , [
' uses ' => ' SongsController@create ' ,
' as ' => ' song.create '
]);
Route:: post ( ' songs ' , [
' uses ' => ' SongsController@store ' ,
' as ' => ' song.store '
]);
使用form()
辅助函数打印视图中的表单:
<!-- resources/views/song/create.blade.php -->
@extends('app')
@section('content')
{!! form($form) !!}
@endsection
转到/songs/create
;上面的代码将生成这个html:
< form method =" POST " action =" http://example.dev/songs " >
< input name =" _token " type =" hidden " value =" FaHZmwcnaOeaJzVdyp4Ml8B6l1N1DLUDsZmsjRFL " >
< div class =" form-group " >
< label for =" name " class =" control-label " > Name </ label >
< input type =" text " class =" form-control " id =" name " >
</ div >
< div class =" form-group " >
< label for =" lyrics " class =" control-label " > Lyrics </ label >
< textarea name =" lyrics " class =" form-control " id =" lyrics " > </ textarea >
</ div >
< div class =" form-group " >
< label for =" publish " class =" control-label " > Publish </ label >
< input type =" checkbox " name =" publish " id =" publish " >
</ div >
</ form >
或者您可以使用简单的数组更轻松地生成表单
<?php
namespace App Http Controllers ;
use Illuminate Routing Controller as BaseController ;
use Kris LaravelFormBuilder FormBuilder ;
use Kris LaravelFormBuilder Field ;
use App Forms SongForm ;
class SongsController extends BaseController {
public function create ( FormBuilder $ formBuilder )
{
$ form = $ formBuilder -> createByArray ([
[
' name ' => ' name ' ,
' type ' => Field:: TEXT ,
],
[
' name ' => ' lyrics ' ,
' type ' => Field:: TEXTAREA ,
],
[
' name ' => ' publish ' ,
' type ' => Field:: CHECKBOX
],
]
,[
' method ' => ' POST ' ,
' url ' => route ( ' song.store ' )
]);
return view ( ' song.create ' , compact ( ' form ' ));
}
}
项目遵循 PSR-2 标准,并包含 PHPUnit 测试。 Pull 请求应包括测试并通过 Travis CI 构建。
要运行测试,首先使用composer install
安装依赖项。
之后可以使用vendor/bin/phpunit
运行测试