承接 bnbwebexpertise/laravel-bootstrap-form 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bnbwebexpertise/laravel-bootstrap-form

Composer 安装命令:

composer require bnbwebexpertise/laravel-bootstrap-form

包简介

Laravel 5 form wrappers for Bootstrap 3. Based on Dwight Watson version and tuned for personal requirements.

README 文档

README

This is a package for simply creating Bootstrap 4 styled form groups in Laravel 5. It extends the normal form builder to provide you with horizontal form groups completed with labels, error messages and appropriate class usage.

For Bootstrap 3, use version 1.x : composer require bnbwebexpertise/laravel-bootstrap-form:^1

Introduction

Simply use the BootstrapForm facade in the place of the Form facade when you want to generate a Bootstrap 4 form group.

BootForm::text('username');

And you'll get back the following:

<div class="form-group">
    <label for="username" class="control-label col-md-2">Username</label>
    <div class="col-md-10">
        <input type="text" name="username" class="form-control">
    </div>
</div>

Of course, if there are errors for that field it will even populate them.

<div class="form-group has-error">
    <label for="username" class="control-label col-md-2">Username</label>
    <div class="col-md-10">
        <input type="text" name="username" class="form-control">
        <span class="help-block">The username field is required.</span>
    </div>
</div>

Installation

First, require the package using Composer.

composer require bnbwebexpertise/laravel-bootstrap-form

For Laravel 5.5 and newer the package is auto-loaded. For older versions, you must configure the providers as described below.

Add these service providers to your config/app.php file (don't add the HtmlServiceProvider if you already have it).

Collective\Html\HtmlServiceProvider::class,
Bnb\BootstrapForm\BootstrapFormServiceProvider::class,

And finally add these to the aliases array (note: Form and Html must be listed before BootstrapForm):

'Form'     => Collective\Html\FormFacade::class,
'HTML'     => Collective\Html\HtmlFacade::class,
'BootForm' => Bnb\BootstrapForm\Facades\BootstrapForm::class,

Feel free to use a different alias for BootstrapForm if you'd prefer something shorter.

Configuration

There are a number of configuration options available for BootstrapForm. Run the following Artisan command to publish the configuration option to your config directory:

php artisan vendor:publish

Horizontal form sizes

When using a horizontal form you can specify here the default sizes of the left and right columns. Note you can specify as many classes as you like for each column for improved mobile responsiveness, for example:

col-md-3 col-sm-6 col-xs-12

Display errors

By default this package will only display the first validation error for each field. If you'd instead like to list out all the validation errors for a field, simply set this configuration option to true.

Required fields

By default this package will append an asterisk * to the label of required fields. It will also add the required CSS class to the form group. You may override these values in the configuration file or by passing the values to the open method :

BootForm::open([ 'label_required_mark' => '(required)', 'group_required_class' => 'field-required' ])

Default values

You can force default values by passing the values to the open method :

BootForm::open([ 'values' => ['foo' => 'bar'] ])

These values take precedence over the model values, but not the provided or the old/request ones.

Usage

When used in a blade template enclose the helper methods inside an unescaped block: {!! !!}

Warning : make sure to escape any character which comes from user input (from any possible source) while using raw ouput tags especially when using explicit HTML version of values.

Opening a form

BootstrapForm has improved the process of opening forms, both in terms of providing Bootstrap classes as well as managing models for model-based forms.

// Passing an existing, persisted model will trigger a model
// binded form.
$user = User::whereEmail('example@example.com')->first();

// Named routes
BootForm::open(['model' => $user, 'store' => 'users.store', 'update' => 'users.update']);

// Controller actions
BootForm::open(['model' => $user, 'store' => 'UsersController@store', 'update' => 'UsersController@update']);

If a model is passed to the open method, it will be configured to use the update route with the PUT method. Otherwise it will point to the store method as a POST request. This way you can use the same opening tag for a form that handles creating and saving.

// Passing a model that hasn't been saved or a null value as the
// model value will trigger a `store` form.
$user = new User;

BootForm::open()

Form variations

There are a few helpers for opening the different kinds of Bootstrap forms. By default, open() will use the the form style that you have set in the configuration file. These helpers take the same input as the open() method.

// Open a vertical Bootstrap form.
BootForm::vertical();

// Open an inline Bootstrap form.
BootForm::inline();

// Open a horizontal Bootstrap form.
BootForm::horizontal();

If you want to change the columns for a form for a deviation from the settings in your configuration file, you can also set them through the $options array.

BootForm::open(['left_column_class' => 'col-md-2', 'left_column_offset_clsas' => 'col-md-offset-2', 'right_column_class' => 'col-md-10']);

Text inputs

Here are the various methods for text inputs. Note that the method signatures are relatively close to those provided by the Laravel form builder but take a parameter for the form label.

// The label will be inferred as 'Username'.
BootForm::text('username');

// The field name by default is 'email'.
BootForm::email();

BootForm::textarea('profile');

// The field name by default is 'password'.
BootForm::password();

Checkbox and radio button inputs

Checkboxes and radio buttons are a little bit different and generate different markup.

View the method signature for configuration options.

// A checked checkbox.
BootForm::checkbox('interests[]', 'Laravel', 'laravel', true);

Same goes for radio inputs.

BootForm::radio('gender', 'Male', 'male');

Multiple checkboxes and radio buttons

By simply passing an array of value/label pairs you can generate a group of checkboxes or radio buttons easily.

$label = 'this is just a label';

$interests = [
    'laravel' => 'Laravel',
    'rails'   => 'Rails',
    'ie6'     => 'Internet Explorer 6'
];

// Checkbox inputs with Laravel and Rails selected.
BootForm::checkboxes('interests[]', $label, $interests, ['laravel', 'rails']);

$genders = [
    'male'   => 'Male',
    'female' => 'Female'
];

// Gender inputs inline, 'Gender' label inferred.
BootForm::radios('gender', null, $genders, null, true);

// Gender inputs with female selected.
BootForm::radios('gender', 'Gender', $genders, 'female');

Submit button

// Pretty simple.
BootForm::submit('Login');

Custom button

// Pretty simple.
BootForm::button('Activate', [ 'data-trigger' => 'foo' ]);

Closing the form

// Pretty simple.
BootForm::close();

Labels

Hide Labels

You may hide an element's label by setting the the value to false.

// An input with no label.
BootForm::text('username', false);

Labels with HTML

To include HTML code inside a label:

// A label with HTML code using array notation
BootForm::text('username', ['html' => 'Username <span class="required">*</span>']);

// A label with HTML code using HtmlString object
BootForm::text('username', new Illuminate\Support\HtmlString('Username <span class="required">*</span>'));

Help Text

You may pass a help_text option to any field to have Bootstrap Help Text appended to the rendered form group.

Form group comment

// The label will be inferred as 'Username'.
BootForm::text('username', null, null, [ 'comment' => 'Please use only letters and numbers' ]);

displays a comment line with bootstrap help-block class :

<div class="form-group">
    <label for="username" class="control-label col-md-2">Username</label>
    <div class="col-md-10">
        <input type="text" name="username" class="form-control">
    </div>
    <p class="help-block">Please use only letters and numbers</p>
</div>

Form group class

// The label will be inferred as 'Username'.
BootForm::text('username', null, null, [ 'form-group-class' => 'my-custom-class' ]);

adds a class to the form-group element :

<div class="form-group my-custom-class">
    <label for="username" class="control-label col-md-2">Username</label>
    <div class="col-md-10">
        <input type="text" name="username" class="form-control">
    </div>
</div>

Static Fields

Display text as static field (replicate input layout) with optionnal default value (supports HTML)

// A static field read from the model
BootForm::staticField('username', 'Username']);

// A static field with explicit value
BootForm::staticField('username', 'Username', $model->login]);

// A static field with explicit value in HTML code using array notation
BootForm::staticField('username', 'Username', ['html' => sprintf('Set to <strong>%s</strong>', e($model->username)]);

bnbwebexpertise/laravel-bootstrap-form 适用场景与选型建议

bnbwebexpertise/laravel-bootstrap-form 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.39k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2016 年 04 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「form」 「bootstrap」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 bnbwebexpertise/laravel-bootstrap-form 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bnbwebexpertise/laravel-bootstrap-form 我们能提供哪些服务?
定制开发 / 二次开发

基于 bnbwebexpertise/laravel-bootstrap-form 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.39k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 4
  • Forks: 139
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-04-29