paulmenheere / laravel-bootstrap-4-forms
Bootstrap 4 form builder for Laravel 5 Forked for development speedup
This package is auto-updated.
Last update: 2024-11-08 17:47:32 UTC
README
This is a package for creating Bootstrap 4 styled form elements in Laravel 5.
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::inlineForm()!!}
Fieldset
// Example {!!Form::fieldsetOpen('Legend title')!!} // ... fieldset content {!!Form::fieldsetClose()!!}
Basic inputs
Text inputs
// Example {!!Form::text('name', 'User name')!!}
Textarea
// Example {!!Form::textarea('description', 'Description')!!}
Select
// Example {!!Form::select('city', 'Choose your city', [1 => 'Gotham City', 2 => 'Springfield'])!!}
Select Default Value
{!!Form::select('city', 'Choose your city', [''=>'--choose your city---',1 => 'Gotham City', 2 => 'Springfield'])!!}
Checkbox
// Example {!!Form::checkbox('orange', 'Orange')!!}
Radio
// Example {!!Form::radio('orange', 'Orange')!!}
File
// Example {!!Form::file('doc', 'Document')!!}
Date inputs
// Example {!!Form::date('birthday', 'Birthday')!!}
Time inputs
// Example {!!Form::time('hour', 'Meeting hour')!!}
Range inputs
// Example {!!Form::range('name', 'User name')!!}
Hidden
// Example {!!Form::hidden('user_id')!!}
Anchor
// Example {!!Form::anchor("Link via parameter", 'foo/bar')!!}
Buttons
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
// 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
// Example {!!Form::anchor("Link via url")->url('foo/bar')!!}
Route
Use in anchors and forms openings
// Example {!!Form::anchor("Link via route")->route('home')!!}
Checked
Set the checkbox/radio 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()!!}
Placeholder
// Example {!!Form::text('name', 'Name')->placeholder('Input placeholder')!!}
Select Multiple
// Example {!!Form::select('city', 'Choose your city', [1 => 'Gotham City', 2 => 'Springfield'])->multiple()!!}
Locale
Using locale, the package will look for a resources/lang/{CURRENT_LANG}/forms/user.php language file and uses labels and help texts as keys for replace texts
// Example {!!Form::open()->locale('forms.user')!!}
Help Text
// Example {!!Form::text('name', 'Name')->help('Help text here')!!}
Custom attributes
// Example {!!Form::text('name', 'Name')->attrs(['data-foo' => 'bar', 'rel'=> 'baz'])!!}
Custom attributes in wrapper div (<div class="form-group">...</div>)
// Example {!!Form::text('name', 'Name')->wrapperAttrs(['data-foo' => 'bar', 'id'=> 'name-wrapper'])!!}
Readonly
// Examples // Using readonly field {!!Form::text('name', 'Name')->readonly()!!} // You can use FALSE to turn off readonly status {!!Form::text('name', 'Name')->readonly(false)!!}
Disabled
// Examples // Disabling a field {!!Form::text('name', 'Name')->disabled()!!} // Disabling a fieldset {!!Form::fieldsetOpen('User data')->disabled()!!} // You can use FALSE to turn off disabled status {!!Form::text('name', 'Name')->disabled(false)!!}
Block
// Examples // Disabling a field {!!Form::text('name', 'Name')->block()!!} // You can use FALSE to turn off block status {!!Form::text('name', 'Name')->block(false)!!}
Required
// Examples // Disabling a field {!!Form::text('name', 'Name')->required()!!} // Disabling a fieldset {!!Form::fieldsetOpen('User data')->required()!!} // You can use FALSE to turn off required status {!!Form::text('name', 'Name')->required(false)!!}
AutoFill
see: https://html.spec.whatwg.org/multipage/forms.html#autofill
If no autocomplete value is specified on the form, html spec requires a default value of 'on'. So, you must explicitly turn it off.
Autocomplete values will be automatically generated for fields with single word names matching valid values (e.g. name, email, tel, organization). The complete list is in the spec mentioned above.
// Examples // Switch off autocomplete for the form {!!Form::open()->autocomplete('off')!!} // Explicitly set a autocomplete value {!!Form::text('mobile', 'Mobile Number')->autocomplete('tel')!!} // Disable autocomplete for fields with valid names {!!Form::text('name', 'Name')->autocomplete('off')!!}
Id
// Example {!!Form::text('name', 'Name')->id('user-name')!!}
Id prefix
// Example {!!Form::open()->idPrefix('register')!!}
Multipart
// Examples {!!Form::open()->multipart()!!} // You can use FALSE to turn off multipart {!!Form::open()->multipart(false)!!}
Method
// Examples {!!Form::open()->method('get')!!} {!!Form::open()->method('post')!!} {!!Form::open()->method('put')!!} {!!Form::open()->method('patch')!!} {!!Form::open()->method('delete')!!}
explicit HTTP verbs
// Examples {!!Form::open()->get()!!} {!!Form::open()->post()!!} {!!Form::open()->put()!!} {!!Form::open()->patch()!!} {!!Form::open()->delete()!!}
Color
// Examples {!!Form::button("Do something")->color("warning")!!} {!!Form::button("Do something")->color("primary")!!}
explicit color
// Examples {!!Form::button("Button label")->warning()!!} {!!Form::button("Button label")->outline()!!} {!!Form::button("Button label")->success()!! {!!Form::button("Button label")->danger()!!} {!!Form::button("Button label")->secondary()!!} {!!Form::button("Button label")->info()!!} {!!Form::button("Button label")->light()!!} {!!Form::button("Button label")->dark()!!} {!!Form::button("Button label")->link()!!}
Size
// Examples {!!Form::button("Do something")->size("sm")!!} {!!Form::button("Do something")->size("lg")!!}
Explicit size
// Examples {!!Form::button("Button label")->sm()!!} {!!Form::button("Button label")->lg()!!}
Type
// Examples // Password field {!!Form::text('password', 'Your password')->type('password')!!} // Number field {!!Form::text('age', 'Your age')->type('number')!!} // Email field {!!Form::text('email', 'Your email')->type('email')!!}
Name
// Examples {!!Form::text('text')->name('name')!!}
Label
// Examples {!!Form::text('age')->label('Your age')!!}
Default Value
// Example {!!Form::text('name', 'Your name')->value('Maria')!!}
Render
// Examples // Number field {!!Form::render('text')->name('age')->label('Your age')!!}
Chaining properties
You can use chaining feature to use a lot of settings for each component
// Examples {!!Form::open()->locale('forms.user')->put()->multipart()->route('user.add')->data($user)!!} {!!Form::text('name', 'Name')->placeholder('Type your name')->lg()!!} {!!Form::anchor("Link as a button")->sm()->info()->outline()!!} {!!Form::submit('Awesome button')->id('my-btn')->disabled()->danger()->lg()!!} {!!Form::close()!!}