当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

laravel-form-builder

Laravel Form builder for version 5+!
授权协议 MIT License
开发语言 PHP
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 酆浩邈
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Laravel 5 form builder

Form 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 4

For Laravel 4 version check laravel4-form-builder.

Bootstrap 4 support

To use bootstrap 4 instead of bootstrap 3, install laravel-form-builder-bs4.

Upgrade to 1.6

If you upgraded to >1.6.* from 1.5.* or earlier, and having problems with form value binding, rename default_value to value.

More info in changelog.

Documentation

For detailed documentation refer to https://kristijanhusak.github.io/laravel-form-builder/.

Changelog

Changelog can be found here.

Installation

Using Composer

composer require kris/laravel-form-builder

Or manually by modifying composer.json file:

{
    "require": {
        "kris/laravel-form-builder": "1.*"
    }
}

And run composer install

Then add Service provider to config/app.php

'providers' => [
        // ...
        Kris\LaravelFormBuilder\FormBuilderServiceProvider::class
    ]

And Facade (also in config/app.php)

'aliases' => [
        // ...
        'FormBuilder' => Kris\LaravelFormBuilder\Facades\FormBuilder::class
    ]

Notice: This package will add laravelcollective/html package and load aliases (Form, Html) if they do not exist in the IoC container.

Quick start

Creating 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 app/Forms/SongForm.php with content:

<?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 --fields parameter:

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 form() helper function:

<!-- resources/views/song/create.blade.php -->

@extends('app')

@section('content')
    {!! form($form) !!}
@endsection

Go to /songs/create; above code will generate this 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>

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'));
    }
}

Contributing

Project 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 composer install.

After that tests can be run with vendor/bin/phpunit

  • 前言 最近在写底代码编程,写到关联关系保存的时候,想一下其实可以参考Laravel-admin 关联保存,因为他很简单的通过 ->hasMany 一个函数就解决了平常我们写的麻烦的关联模型。所以别人优秀的代码和思想是值得借鉴的。 查找源码位置 关联保存肯定是在Form 模块中编写的,所以我很快的定位到了 vendor\encore\laravel-admin\src\Form.php 在updat

  • laravel-admin恶心的图片上传功能,让广大开发者很是苦恼,今天分享一个 手动改为异步传图的完整方案给大家,如果有大神在,也可以考虑将其封装为扩展~ 同时也分享一下列表使用tab的解决方案~以及如何使用json方式存储动态性字段比较强的类型的数据~ 话不多,看代码 grid()控制器 protected function grid() { $grid = new Grid(new Ad()

  • 问题地址(shinbashi回答): https://github.com/z-song/laravel-admin/issues/899 相关问题地址: https://github.com/z-song/laravel-admin/issues/2889 首先该问题出现的原因是因为原框架不支持在hasMany中使用内嵌方法embeds,至于为什么没有加入到新版本中,原因也不知道了,这里提供仅仅

  • 1)列表页List <?php namespace App\Admin\Controllers\Users; use Encore\Admin\Form; use Encore\Admin\Grid; use Encore\Admin\Show; use App\Models\Users; /** * Make a grid builder. * * @return Grid */

  • 我的个人博客:逐步前行STEP 有时候我们使用Laravel-admin管理数据时,需要保存一些通过程序运算出来的数据,而不只是存储写在表单中的数据,也就是需要在保存数据前可以设置或改变数据。 比如存在这么个需求: 为了快速创建\管理一些测试数据,在Admin管理端创建一个用户账户的同时,创建相关的用户信息,用户订单、地址库等等一系列信息。 以这个需求中用户关联的数据量来说,每个数据都手动输入的话

  • @[TOC]( laravel-admin V1.7后$from-deleting()不回调解决方法 ) laravel-admin V1.7后$from-deleting()不回调解决方法 解决方法 第一步 先在对应的TestController 里创建一个form()方 <?php namespace App\Admin\Controllers; use App\Models\Test;

 相关资料
  • Laravel Form Components A set of Blade components to rapidly build forms with Tailwind CSS v1, Tailwind CSS v2, Bootstrap 4 and Bootstrap 5. Supports validation, model binding, default values, transla

  • 我面临“类”表单“未找到”的问题,我目前正在使用laravel 5.4。我已经尽了最大的努力来解决这个问题。 谢谢 错误是:哎呀,看起来好像出了什么问题。 1/1 d0b19e04e5a1f8a5507d8ca427362b23807103ca.php行23中的FatalErrorException:在d0b19e04e5a1f8a5507d8ca427362b23807103ca.php行23中

  • 所以我有这个在我的视图中,我想限制文件上载到doc、ppt、pdf、docx、xls、xlsx、txt等。所以我的代码是这样的 但点击后,打开的窗口仍显示视频、音频和其他文件类型。对如何正确地做到这一点有什么想法吗?提前道谢! 如果用户输入的文件类型不被接受,编辑我已经有一个后端检查器。我只是想把用户的选择范围缩小到一个正确的文件类型,当他点击浏览文件。谢谢

  • 1.7.0 新增 从 1.8.0 开始支持blur 时才触发校验以及 debounce,同 Validator 一样也开始支持异步校验。 表单,包含各种输入组件以及对应的校验;我们可以通过数据驱动的方式来生成完成表单。 示例 默认配置使用 一个完整的包含所有的内置表单相关组件。 <cube-form :model="model" :schema="schema" :immediate-

  • 此方法返回'NUMERIC FORM'的当前设置,该设置用于在系统上进行数学计算。 语法 (Syntax) FORM() 参数 (Parameters) 没有 返回值 (Return Value) 此方法返回'NUMERIC FORM'的当前设置,该设置用于在系统上进行数学计算。 例子 (Example) /* Main program */ say FORM() 当我们运行上述程序时,

  • 在大多数Web应用程序中,表单是从用户获取信息的最重要的小部件,例如登录表单/反馈表单,以便可以将值保存在数据库中以供将来参考。 表单窗口小部件用于此目的。 在创建表单之前,我们应该了解xTypes。 xType定义Ext JS UI组件的类型,该组件在呈现组件期间确定。 例如,元素可以是我们将xType作为textField的文本框,或者元素只能具有我们具有Numeric xType的数值。 不