Skip to content

Laravel Collective 5 based Twitter Bootstrap 3 Form service

License

Notifications You must be signed in to change notification settings

core-system/bootstrap-form

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CORE-SYSTEM BOOTSTRAP FORM

Packagist GitHub release

This is a standalone part of core-system application for Laravel 5 Framework.

CORE-SYSTEM is Laravel 5 based application

CORE-SYSTEM Bootstrap Form contains laravel-collective/html extension for simple Twitter Bootstrap 3 form generation and and request validation error handling.

Summary

Licence

GPL-3.0+

Requirements and dependencies

This package uses composer to installing dependencies

Composer

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

Installation

Run terminal. Go to your web projects root directory and type following composer create-project command and install new installation of Laravel Framework

Skip this command if you have Laravel framework already installed

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

Open composer.json file located in your project folder and add following lines to require key

For laravelcollective/html - 5.3 and bellow versions please use "core-system/bootstrap-form": "1.0.*" because there is some changes in laravelcollective/html API between 5.3 and 5.4 release

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

Run composer update command

$ composer update

Go to your Laravel config/app.php and add this line to your providers key

Core\BootstrapForm\BootstrapFormServiceProvider::class 

and following line to aliases key to register Laravel Collective form facade

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

If you need you can config control and error class publish vendor config file

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

Usage

In Blade templates you can use this extensions as standard Laravel Collective Form.

Form methods

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

create <form> tag with given attributes and <input type="hidden"> for Laravel cross site request forgery protection

Form::close()

create </form> closing tag

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

create opening .form-group element with given attributes

Form::closeGroup()

close cloning tag for actually opened .form-group element

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

create <input> field

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

create <select> field

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

create plain <input> field

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

create plain <select> field

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

create <input type="checkbox"> field

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

create <input type="radio"> field

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

create in-line <input type="checkbox"> field

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

create in-line <input type="radio"> field

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

create <textarea> field

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

create plain <textarea> field

Basic Blade template usage example

{!! 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() !!}

Validation

U can use standard Laravel request validation methods. Package render error classes for Twitter Bootstrap 3 automatically.

Laravel 5 request validation example

Create new request class file via php artisan make:request <className> command

$ php artisan make:request LoginFormRequest

In created LoginFormRequest.php file which is located in your app/Http/Requests folder change content to following lines

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'
        ];
    }
}

And in you controller add request to post action parameters via Laravel 5 dependency injection

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
    }
}