核心系统/引导表单

基于 Laravel Collective 5 的 Twitter Bootstrap 3 表单服务。

v1.1.2 2018-12-07 22:26 UTC

This package is not auto-updated.

Last update: 2024-09-23 07:07:27 UTC


README

Packagist GitHub release

这是 Laravel 5 框架应用程序 core-system 的独立部分。

CORE-SYSTEM 是基于 Laravel 5 的应用程序

CORE-SYSTEM 引导表单包含用于简单 Twitter Bootstrap 3 表单生成和请求验证错误处理的 laravel-collective/html 扩展。

摘要

许可证

GPL-3.0+

要求和依赖

此包使用 composer 安装依赖项

Composer

  • "php": ">=5.5.9"
  • "laravel/laravel": "^5.2"
  • "laravelcollective/html": "^5.0"

安装

运行终端。转到您的 Web 项目根目录,并输入以下 composer create-project 命令以安装 Laravel 框架的新安装

如果您已经安装了 Laravel 框架,请跳过此命令

$ composer create-project laravel/laravel your-project-name --prefer-dist

打开位于项目文件夹中的 composer.json 文件,并将以下行添加到 require

对于 laravelcollective/html - 5.3 及以下版本,请使用 "core-system/bootstrap-form": "1.0.*",因为 laravelcollective/html API 在 5.35.4 发布之间有所变化

{
    "require": {
        "core-system/bootstrap-form": "1.1.*"
    }
}

运行 composer update 命令

$ composer update

转到您的 Laravel config/app.php 文件,并将此行添加到您的 providers

Core\BootstrapForm\BootstrapFormServiceProvider::class 

并添加以下行到 aliases 键以注册 Laravel Collective 表单外观

'Form' => Collective\Html\FormFacade::class

如果您需要,可以配置控制类和错误类以发布供应商配置文件

$ php artisan vendor:publish --provider="Core\BootstrapForm\BootstrapFormServiceProvider" --tag="config"

使用

在 Blade 模板中,您可以像使用标准 Laravel Collective 表单一样使用这些扩展。

表单方法

Form::open(array $options = [])

创建具有给定属性的 <form> 标签和用于 Laravel 跨站点请求伪造保护的 <input type="hidden">

Form::close()

创建 </form> 关闭标签

Form::openGroup(string $name, mixed $label = null, array $options = [])

创建具有给定属性的打开 .form-group 元素

Form::closeGroup()

关闭当前打开的 .form-group 元素的克隆标签

Form::input(string $type, string $name, mixed $value = null, array $options = [])

创建 <input> 字段

Form::select(string $name, array $list = [], mixed $selected, array $selectAttributes = [], array $optionsAttributes = [])

创建 <select> 字段

Form::plainInput(string $type, string $name, mixed $value = null, array $options = [])

创建纯 <input> 字段

Form::plainSelect(string $name, array $list = [], mixed $selected, array $selectAttributes = [], array $optionsAttributes = [])

创建纯 <select> 字段

Form::checkbox(string $name, mixed $value = 1, mixed $label = null, bool $checked = null, array $options = [])

创建 <input type="checkbox"> 字段

Form::radio(string $name, mixed $value = null, mixed $label = null, bool $checked = null, array $options = [])

创建 <input type="radio"> 字段

Form::inlineCheckbox(string $name, mixed $value = 1, mixed $label = null, bool $checked = null, array $options = [])

创建内联 <input type="checkbox"> 字段

Form::inlineRadio(string $name, mixed $value = null, mixed $label = null, bool $checked = null, array $options = [])

创建内联 <input type="radio"> 字段

Form::textarea(string $name, mixed $value = null, array $options = [])

创建 <textarea> 字段

Form::plainTextarea(string $name, mixed $value = null, array $options = [])

创建纯 <textarea> 字段

基本 Blade 模板使用示例

{!! Form::open(['class' => 'form', 'id' => 'loginForm', 'url' => route('backend.auth.login')]) !!}
    {!! Form::openGroup('email', null, ['class' => 'has-feedback']) !!}
        {!! Form::text('email', null, ['placeholder' => trans('auth.email-placeholder') ]) !!}
    {!! Form::closeGroup() !!}
    {!! Form::openGroup('password', null, ['class' => 'has-feedback']) !!}
        {!! Form::password('password', ['placeholder' => trans('auth.password-placeholder') ]) !!}
    {!! Form::closeGroup() !!}
    {!! Form::openGroup('submit', null) !!}
        {!! Form::input('submit', 'submit', trans('auth.login'), ['class' => 'btn btn-primary btn-lg']) !!}
    {!! Form::closeGroup() !!}
{!! Form::close() !!}

验证

您可以使用标准的 Laravel 请求验证方法。该包会自动渲染 Twitter Bootstrap 3 的错误类。

Laravel 5 请求验证示例

通过 php artisan make:request <className> 命令创建新的请求类文件

$ php artisan make:request LoginFormRequest

在位于您的 app/Http/Requests 文件夹中的 LoginFormRequest.php 文件中更改内容如下

namespace App\Http\Requests;

use App\Http\Requests\Request;

class LoginFormRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'email' => 'required|email',
            'password' => 'required|min:6'
        ];
    }
}

并在您控制器中,通过 Laravel 5 依赖注入将请求添加到 post 操作参数中

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests\LoginFormRequest;

class FormsController extends Controller
{
    /**
     * Example action to handle login form POST action
     *
     * @return void
     */     
    public function login(LoginFormRequest $request)
    {
        // your next logical code
    }
}