PHP code example of netojose / laravel-bootstrap-4-forms
1. Go to this page and download the library: Download netojose/laravel-bootstrap-4-forms library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
netojose / laravel-bootstrap-4-forms example snippets
// Opening a form using POST method
{!!Form::open()!!}
// ... Form components here
{!!Form::close()!!}
// Making all inputs inline
{!!Form::open()->formInline()!!}
// You can use FALSE to turn off disable form inline
{!!Form::open()->formInline(false)!!}
// Example
{!!Form::fieldsetOpen('Legend title')!!}
// ... fieldset content
{!!Form::fieldsetClose()!!}
// Example
{!!Form::text('name', 'User name')!!}
// Example
{!!Form::textarea('description', 'Description')!!}
// Example
{!!Form::select('city', 'Choose your city', [1 => 'Gotham City', 2 => 'Springfield'])!!}
// 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', ''))!!}
// Example
{!!Form::checkbox('orange', 'Orange')!!}
// Example
{!!Form::radio('orange', 'Orange')!!}
// Example
{!!Form::file('doc', 'Document')!!}
// Example
{!!Form::date('birthday', 'Birthday')!!}
// Example
{!!Form::tel('number', 'Phone number')!!}
// Example
{!!Form::time('hour', 'Meeting hour')!!}
// Example
{!!Form::urlInput('website', 'You website')!!}
// Example
{!!Form::range('name', 'User name')!!}
// Example
{!!Form::hidden('user_id')!!}
// Example
{!!Form::anchor("Link via parameter", 'foo/bar')!!}
// Example
{!!Form::submit("Send form")!!}
// Example
{!!Form::button("Do something", "warning", "lg")!!}
// Example
{!!Form::reset("Clear 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)!!}
// Example
{!!Form::anchor("Link via url")->url('foo/bar')!!}
// Example
{!!Form::anchor("Link via route")->route('home')!!}
// 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
];
}
}
// Example
{!!Form::errors("The form has errors")!!}
// 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)!!}
// 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)!!}
// 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)!!}
// Example
{!!Form::text('name', 'Name')->placeholder('Input placeholder')!!}
// Example
{!!Form::select('city', 'Choose your city', [1 => 'Gotham City', 2 => 'Springfield'])->multiple()!!}
// Example
{!!Form::open()->locale('forms.user')!!}
// Example
{!!Form::text('name', 'Name')->help('Help text here')!!}
// Example
{!!Form::text('name', 'Name')->attrs(['data-foo' => 'bar', 'rel'=> 'baz'])!!}
// Example
{!!Form::text('name', 'Name')->wrapperAttrs(['data-foo' => 'bar', 'id'=> 'name-wrapper'])!!}
// Examples
// Using readonly field
{!!Form::text('name', 'Name')->readonly()!!}
// You can use FALSE to turn off readonly status
{!!Form::text('name', 'Name')->readonly(false)!!}
// 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)!!}
// Examples
// Disabling a field
{!!Form::text('name', 'Name')->block()!!}
// You can use FALSE to turn off block status
{!!Form::text('name', 'Name')->block(false)!!}
// Examples
// Disabling a field
{!!Form::text('name', 'Name')->uired()!!}
// You can use FALSE to turn off
// 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')!!}
// Example
{!!Form::text('name', 'Name')->id('user-name')!!}
// Example
{!!Form::open()->idPrefix('register')!!}
// Examples
{!!Form::open()->multipart()!!}
// You can use FALSE to turn off multipart
{!!Form::open()->multipart(false)!!}
// 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')!!}
// Example
{!!Form::text('age', 'Your age')->type('number')->min(18)!!}
// Example
{!!Form::text('age', 'Your age')->type('number')->max(18)!!}
// Example
{!!Form::text('name', 'Your name')->value('Maria')!!}
// Examples
// Number field
{!!Form::render('text')->name('age')->label('Your age')!!}
// Examples
// Disable Bootstrap's is-valid CSS class
{!!Form::text('name', 'Name')->disableIsValid()!!}
// 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()!!}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.