Laravel 5 的表單建構器,靈感來自 Symfony 的表單建構器。在 Laravels FormBuilder 類別的幫助下,建立可以輕鬆修改和重複使用的表單。預設情況下它支援 Bootstrap 3。
對於 Laravel 4 版本,請檢查 laravel4-form-bilder。
要使用 bootstrap 4 而不是 bootstrap 3,請安裝 laravel-form-byilder-bs4。
如果您從1.5.*
>1.6.*
*,並且在表單值綁定方面遇到問題,請將default_value
重新命名為value
。
更多資訊請參閱變更日誌。
有關詳細文檔,請參閱 https://kristijanhusak.github.io/laravel-form-byilder/。
可以在此處找到更改日誌。
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
執行測試