在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):kristijanhusak/laravel-form-builder开源软件地址(OpenSource Url):https://github.com/kristijanhusak/laravel-form-builder开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Laravel 5 form builderForm builder for Laravel 5 inspired by Symfony's form builder. With help of Laravels FormBuilder class creates forms that can be easy modified and reused. By default it supports Bootstrap 3. Laravel 4For Laravel 4 version check laravel4-form-builder. Bootstrap 4 supportTo use bootstrap 4 instead of bootstrap 3, install laravel-form-builder-bs4. Upgrade to 1.6If you upgraded to More info in changelog. DocumentationFor detailed documentation refer to https://kristijanhusak.github.io/laravel-form-builder/. ChangelogChangelog can be found here. InstallationUsing Composercomposer require kris/laravel-form-builder Or manually by modifying {
"require": {
"kris/laravel-form-builder": "1.*"
}
} And run Then add Service provider to 'providers' => [
// ...
Kris\LaravelFormBuilder\FormBuilderServiceProvider::class
] And Facade (also in 'aliases' => [
// ...
'FormBuilder' => Kris\LaravelFormBuilder\Facades\FormBuilder::class
] Notice: This package will add Quick startCreating form classes is easy. With a simple artisan command: php artisan make:form Forms/SongForm --fields="name:text, lyrics:textarea, publish:checkbox" Form is created in path <?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);
}
} If you want to instantiate empty form without any fields, just skip passing php artisan make:form Forms/PostForm Gives: <?php
namespace App\Forms;
use Kris\LaravelFormBuilder\Form;
class PostForm extends Form
{
public function buildForm()
{
// Add fields here...
}
} After that instantiate the class in the controller and pass it to view: <?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...
}
} Alternative example: <?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...
}
} If you want to store a model after a form submit considerating all fields are model properties: <?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...
} You can only save properties you need: <?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...
} Or you can update any model after form submit: <?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...
} Create the routes // app/Http/routes.php
Route::get('songs/create', [
'uses' => 'SongsController@create',
'as' => 'song.create'
]);
Route::post('songs', [
'uses' => 'SongsController@store',
'as' => 'song.store'
]); Print the form in view with <!-- resources/views/song/create.blade.php -->
@extends('app')
@section('content')
{!! form($form) !!}
@endsection Go to <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> Or you can generate forms easier by using simple array <?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'));
}
} ContributingProject follows PSR-2 standard and it's covered with PHPUnit tests. Pull requests should include tests and pass Travis CI build. To run tests first install dependencies with After that tests can be run with |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论