Pembuat formulir untuk Laravel 5 terinspirasi oleh pembuat formulir Symfony. Dengan bantuan kelas Laravels FormBuilder membuat formulir yang mudah dimodifikasi dan digunakan kembali. Secara default mendukung Bootstrap 3.
Untuk versi Laravel 4 periksa laravel4-form-builder.
Untuk menggunakan bootstrap 4 alih-alih bootstrap 3, instal laravel-form-builder-bs4.
Jika Anda meningkatkan ke >1.6.*
dari 1.5.*
atau yang lebih lama, dan mengalami masalah dengan pengikatan nilai formulir, ganti nama default_value
menjadi value
.
Info lebih lanjut di log perubahan.
Untuk dokumentasi detail, lihat https://kristijanhusak.github.io/laravel-form-builder/.
Changelog dapat ditemukan di sini.
composer require kris/laravel-form-builder
Atau secara manual dengan memodifikasi file composer.json
:
{
"require" : {
"kris/laravel-form-builder" : " 1.* "
}
}
Dan jalankan composer install
Kemudian tambahkan Penyedia layanan ke config/app.php
' providers ' => [
// ...
Kris LaravelFormBuilder FormBuilderServiceProvider::class
]
Dan Facade (juga di config/app.php
)
' aliases ' => [
// ...
' FormBuilder ' => Kris LaravelFormBuilder Facades FormBuilder::class
]
Pemberitahuan : Paket ini akan menambahkan paket laravelcollective/html
dan memuat alias (Form, Html) jika tidak ada di container IoC.
Membuat kelas formulir itu mudah. Dengan perintah artisan sederhana:
php artisan make:form Forms/SongForm --fields= " name:text, lyrics:textarea, publish:checkbox "
Formulir dibuat di jalur app/Forms/SongForm.php
dengan konten:
<?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 );
}
}
Jika Anda ingin membuat instance formulir kosong tanpa kolom apa pun, lewati saja parameter --fields
:
php artisan make:form Forms/PostForm
Memberikan:
<?php
namespace App Forms ;
use Kris LaravelFormBuilder Form ;
class PostForm extends Form
{
public function buildForm ()
{
// Add fields here...
}
}
Setelah itu buat instance kelas di pengontrol dan teruskan ke tampilan:
<?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...
}
}
Contoh alternatif:
<?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...
}
}
Jika Anda ingin menyimpan model setelah formulir dikirimkan dengan mempertimbangkan semua bidang adalah properti model:
<?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...
}
Anda hanya dapat menyimpan properti yang Anda perlukan:
<?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...
}
Atau Anda dapat memperbarui model apa pun setelah formulir dikirimkan:
<?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...
}
Buat rutenya
// app/Http/routes.php
Route:: get ( ' songs/create ' , [
' uses ' => ' SongsController@create ' ,
' as ' => ' song.create '
]);
Route:: post ( ' songs ' , [
' uses ' => ' SongsController@store ' ,
' as ' => ' song.store '
]);
Cetak formulir dalam tampilan dengan fungsi pembantu form()
:
<!-- resources/views/song/create.blade.php -->
@extends('app')
@section('content')
{!! form($form) !!}
@endsection
Pergi ke /songs/create
; kode di atas akan menghasilkan html ini:
< 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 >
Atau Anda dapat membuat formulir lebih mudah dengan menggunakan array sederhana
<?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 ' ));
}
}
Proyek mengikuti standar PSR-2 dan ditutupi dengan tes PHPUnit. Permintaan penarikan harus mencakup pengujian dan lulus build Travis CI.
Untuk menjalankan tes, pertama-tama instal dependensi dengan composer install
.
Setelah itu pengujian dapat dijalankan dengan vendor/bin/phpunit