• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

netojose/laravel-bootstrap-4-forms: Bootstrap 4 forms for Laravel 5/6/7/8

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

netojose/laravel-bootstrap-4-forms

开源软件地址(OpenSource Url):

https://github.com/netojose/laravel-bootstrap-4-forms

开源编程语言(OpenSource Language):

PHP 100.0%

开源软件介绍(OpenSource Introduction):

Bootstrap 4 forms for Laravel 5/6/7/8

Latest Version on Packagist Total Downloads

This is a package for creating Bootstrap 4 styled form elements in Laravel 5/6.

Features

  • Labels
  • Error messages
  • Bootstrap 4 markup and classes (including state, colors, and sizes)
  • Error validation messages
  • Form fill (using Model instance, array or after form submission when a validation error occurs)
  • Internationalization
  • Add parameters using php chaining approach
  • Zero dependences (no Laravel Collective dependency)

Introduction

Before

<div class="form-group">
    <label for="username">Username</label>
    <input
        type="text"
        class="form-control @if($errors->has('username')) is-invalid @endif "
        id="username"
        value="{{old('username', $username)}}"
    />
    @if($errors->has('username'))
    <div class="invalid-feedback">{{$errors->first('username')}}</div>
    @endif
</div>

After

Form::text('username', 'Username')

Installation

Require the package using Composer.

composer require netojose/laravel-bootstrap-4-forms

Laravel 5.5 or above

If you is using Laravel 5.5, the auto discovery feature will make everything for you and your job is done, you can start using now. Else, follow the steps below to install.

Laravel 5.4

Add the service provider to your config/app.php file

'providers' => [
    //...
	NetoJose\Bootstrap4Forms\Bootstrap4FormsServiceProvider::class,
],

Add the BootForm facade to the aliases array in config/app.php:

'aliases' => [
    //...
    'Form' => NetoJose\Bootstrap4Forms\Bootstrap4FormsFacade::class,
],

Usage

Basic form controls

Opening and closing a form

// Opening a form using POST method

{!!Form::open()!!}
// ... Form components here
{!!Form::close()!!}

Opening the form will add _token field automatically for you

Inline form

// Making all inputs inline
{!!Form::open()->formInline()!!}

// You can use FALSE to turn off disable form inline
{!!Form::open()->formInline(false)!!}

Fieldset

Param Type Default Description
$legend string null Fieldset Legend
// Example
{!!Form::fieldsetOpen('Legend title')!!}
// ... fieldset content
{!!Form::fieldsetClose()!!}

Basic inputs

Text inputs

Param Type Default Description
$name string null Input name
$label string null Input label
$default string null Default value
// Example
{!!Form::text('name', 'User name')!!}
Textarea
Param Type Default Description
$name string null Input name
$label string null Input label
$default string null Default value
// Example
{!!Form::textarea('description', 'Description')!!}
Select
Param Type Default Description
$name string null Input name
$label string null Input label
$options array [] Select options
$default string null Default value
// Example
{!!Form::select('city', 'Choose your city', [1 => 'Gotham City', 2 => 'Springfield'])!!}
Options
Param Type Default Description
$options iterable [] Options list
$valueKey string null key for value
$idKey string null key for id
// Example

// With array
{!!Form::select('city', 'Choose your city')->options([1 => 'Gotham City', 2 => 'Springfield'])!!}

// With collection
$cities = collect([1 => 'Gotham City', 2 => 'Springfield'])
{!!Form::select('city', 'Choose your city')->options($cities)!!}

// With model collection
$cities = \App\City::all();
{!!Form::select('city', 'Choose your city')->options($cities)!!}

// Your model should have id and name attributes. If these keys are different, you can pass second and/or third parameters (you can use the second parameter to access some model acessor, also)
$cities = \App\City::all();
{!!Form::select('city', 'Choose your city')->options($cities, 'city_name', 'id_object_field')!!}

// When you are using collections, you can use prepend method (https://laravel.com/docs/5.8/collections#method-prepend) to add an first empty value, like "Choose your city"
$cities = \App\City::all();
{!!Form::select('city', 'Choose your city')->options($cities->prepend('Choose your city', ''))!!}
Checkbox
Param Type Default Description
$name string null Input name
$label string null Input label
$value string null Input value
$checked boolean null Default value
// Example
{!!Form::checkbox('orange', 'Orange')!!}
Radio
Param Type Default Description
$name string null Input name
$label string null Input label
$value string null Input value
$checked boolean null Default value
// Example
{!!Form::radio('orange', 'Orange')!!}
File
Param Type Default Description
$name string null Input name
$label string null Input label
// Example
{!!Form::file('doc', 'Document')!!}

Date inputs

Param Type Default Description
$name string null Input name
$label string null Input label
$default string null Default value
// Example
{!!Form::date('birthday', 'Birthday')!!}

Tel inputs

Param Type Default Description
$name string null Input name
$label string null Input label
$default string null Default value
// Example
{!!Form::tel('number', 'Phone number')!!}

Time inputs

Param Type Default Description
$name string null Input name
$label string null Input label
$default string null Default value
// Example
{!!Form::time('hour', 'Meeting hour')!!}

URL inputs

Param Type Default Description
$name string null Input name
$label string null Input label
$default string null Default value
// Example
{!!Form::urlInput('website', 'You website')!!}

Range inputs

Param Type Default Description
$name string null Input name
$label string null Input label
$default string null Default value
// Example
{!!Form::range('name', 'User name')!!}
Hidden
Param Type Default Description
$name string null Input name
$default boolean null Default value
// Example
{!!Form::hidden('user_id')!!}
Anchor
Param Type Default Description
$value string null Anchor text
$url string null Anchor url
// Example
{!!Form::anchor("Link via parameter", 'foo/bar')!!}
Buttons
Param Type Default Description
$value string null Button value
$color string null Button color
$size string null button size
Submit
// Example
{!!Form::submit("Send form")!!}
Button
// Example
{!!Form::button("Do something", "warning", "lg")!!}
Reset
// Example
{!!Form::reset("Clear form")!!}

Chainable methods

This package uses chaining feature, allowing easly pass more parameters.

Filling a form

Param Type Default Description
$data object/array array Data fo fill form inputs
// Examples

// With initial data using a Model instance
$user = User::find(1);
{!!Form::open()->fill($user)!!}

// With initial array data
$user = ['name' => 'Jesus', 'age' => 33];
{!!Form::open()->fill($user)!!}

Url

Use in anchors and forms openings

Param Type Default Description
$url string null Url
// Example
{!!Form::anchor("Link via url")->url('foo/bar')!!}

Route

Use in anchors and forms openings

Param Type Default Description
$route string null Route name
// Example
{!!Form::anchor("Link via route")->route('home')!!}

Error Bag

Use if you have more then one form per page. You set an identifier for each form, and the errors will be attached for that specific form

Param Type Default Description
$value string null Error bag name
// Example: attach this form to a error bag called "registerErrorBag"
{!!Form::open()->route('register.post')->errorBag("registerErrorBag")!!}

// ------------------------------------------------------

// Now, in your controller (register.post route), you can redirect the user to a form page again, with erros inside a error bag called "registerErrorBag"
public function register(Request $request)
{
    $validator = Validator::make($request->all(), [
        // ... rules here
    ]);

    if ($validator->fails()) {
        return redirect()
            ->route('register.form')
            ->withInput()
            ->withErrors($validator, 'registerErrorBag');
    }

    // Proced to register here
}

// ------------------------------------------------------

// If your validation is on a Form Request, you can add a protected method "$errorBag" to set a ErrorBag name

class RegisterRequest extends FormRequest
{

    protected $errorBag = 'registerErrorBag';

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            // ... rules here
        ];
    }
}

Errors

Show all errors inside a panel

Param Type Default Description
$title string null Panel title
// Example
{!!Form::errors("The form has errors")!!}

Disable validation messages

Disable success/error status and validation error message

Param Type Default Description
$disabled boolean false Disabled status
// Example
{!!Form::text('username', 'User name')->disableValidation()!!}

// You can use FALSE to turn off disable validation (to enable it)
{!!Form::text('username', 'User name')->disableValidation(false)!!}

Checked

Set the checkbox/radio checked status

Param Type Default Description
$checked boolean true Checked status
// Examples

// Using readonly field
{!!Form::checkbox('agree', 'I agree')->checked()!!}

// You can use FALSE to turn off checked status
{!!Form::checkbox('agree', 'I agree')->checked(false)!!}

Inline

Set the checkbox/radio checked status

// Examples
{!!Form::radio('orange', 'Orange')->inline()!!}

{!!Form::checkbox('orange', 'Orange')->inline()!!}

// You can use FALSE to turn off inline status
{!!Form::checkbox('orange', 'Orange')->inline(false)!!}

Placeholder

Param Type Default Description
$placeholder string null Placeholder text
// Example
{!!Form::text('name', 'Name')->placeholder('Input placeholder')!!}

热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap