laravel-form-builder 高效的创建表单

做一个系统免不了要有CURD操作, 表单的创建更是稀松平常, 以前都是一个一个表单的去写, 有很多代码都是重复的, 效率很低, 现在好了, 有了laravel-form-builder, 妈妈再也不用操心我的表单了. ] https://github.com/kristijanhusak/laravel-form-builder ] https://github.com/kristijanhusak/laravel-form-builder/tree/laravel-4

2. 安装{ "require": { "kris/laravel-form-builder": "0.*" } }'providers' => [ // ... 'KrisLaravelFormBuilderFormBuilderServiceProvider' ]'aliases' => [ // ... 'FormBuilder' => 'KrisLaravelFormBuilderFacadesFormBuilder' ]
php artisan form:make app/Project/Forms/PostForm 创建一个表单类文件<?php namespace ProjectForms;

use KrisLaravelFormBuilderForm;

class PostForm extends Form
{
    public function buildForm()
    {
        // Add fields here...
    }
}生成以下的文件:<?php namespace ProjectForms;

use KrisLaravelFormBuilderForm;

class SongForm extends Form
{
    public function buildForm()
    {
        $this
            ->add('name', 'text')
            ->add('lyrics', 'textarea')
            ->add('publish', 'checkbox');
    }
}<?php namespace ProjectHttpControllers;

use IlluminateRoutingController;

class SongsController extends BaseController {

    /**
     * @Get("/songs/create", as="song.create")
     */
    public function index()
    {
        $form = FormBuilder::create('ProjectFormsSongForm', [
            'method' => 'POST',
            'url' => route('song.store')
        ]);

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

    /**
     * @Post("/songs", as="song.store")
     */
    public function store()
    {
    }
}
<!-- resources/views/song/create.blade.php -->

@extend('layouts.master')

@section('content')
    {{ form($form) }}
@endsection<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"></textarea>
    </div>
    <div class="form-group">
        <label for="publish" class="control-label">publish</label>
        <input type="checkbox" name="publish" id="publish">
    </div>
</form>

更多的使用方法, 请参考官方文档.

转载请注明:代码家园 » laravel-form-builder 高效的创建表单

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)