项目作者: Elnooronline

项目描述 :
Provides an easy way to use bootstrap form components within your Laravel projects.
高级语言: PHP
项目地址: git://github.com/Elnooronline/laravel-bootstrap-forms.git
创建时间: 2018-02-21T15:38:11Z
项目社区:https://github.com/Elnooronline/laravel-bootstrap-forms

开源协议:

下载


This package has been deprecated. But worry not. You can use laraeast/laravel-bootstrap-forms

# Laravel Bootstrap Forms.


Build Status
Total Downloads
Latest Stable Version
License

# Installation

Begin by installing this package through Composer. Edit your project’s composer.json file to require elnooronline/laravel-bootstrap-forms.

  1. composer require elnooronline/laravel-bootstrap-forms

You should publish the flags icons in public path to display in multilingual form tabs.

  1. php artisan vendor:publish --tag=locales:flags

# Opening A Form

  1. {{ BsForm::open(['url' => 'foo/bar']) }}
  2. //
  3. {{ BsForm::close() }}

By default, a POST method will be assumed; however, you are free to specify another method:

  1. {{ BsForm::open(['url' => 'foo/bar', 'method' => 'post']) }}

Note: Since HTML forms only support POST and GET, PUT and DELETE methods will be spoofed by automatically adding a _method hidden field to your form.

You may also open forms with method as well:

  1. {{ BsForm::get('foo/bar') }}
  2. {{ BsForm::post('foo/bar') }}
  3. {{ BsForm::put('foo/bar') }}
  4. {{ BsForm::patch('foo/bar') }}
  5. {{ BsForm::delete('foo/bar') }}
  6. {{ BsForm::model($model, 'foo/bar') }}
  7. {{ BsForm::putModel($model, 'foo/bar') }}
  8. {{ BsForm::patchModel($model, 'foo/bar') }}

You may also open forms that point to named routes or controller actions:

  1. {{ BsForm::open(['route' => 'route.name']) }}
  2. {{ BsForm::open(['action' => 'Controller@method']) }}

You may pass in route parameters as well:

  1. {{ BsForm::open(['route' => ['route.name', $user->id]]) }}
  2. {{ BsForm::open(['action' => ['Controller@method', $user->id]]) }}

# Text, Text Area, Date, Number & Password Fields

Generating A Text Input

  1. {{ BsForm::text('username') }}

Specifying A Default Value

  1. {{ BsForm::text('email', 'example@gmail.com') }}
  2. {{ BsForm::text('email')->value('example@gmail.com') }}

Note: The date, number and textarea methods have the same signature as the text method.

Generating A Password Input

  1. {{ BsForm::password('password', ['class' => 'awesome']) }}
  2. {{ BsForm::password('password')->attr('class', 'awesome') }}

Generating Other Inputs

  1. {{ BsForm::email($name)->value($value)->label($label) }}
  2. {{ BsForm::file($name)->label('Upload File') }}

# Checkboxes and Radio Buttons

Generating A Checkbox Or Radio Input

  1. {{ BsForm::checkbox('name', 'value')->checked(false) }}
  2. {{ BsForm::checkbox('name')->value('value')->checked(false) }}
  3. {{ BsForm::radio('name', 'value')->checked(false)->label('label') }}
  4. {{ BsForm::radio('name')->value('value')->checked(false)->label('label') }}

# Drop-Down Lists

  1. {{ BsForm::select('size', ['L' => 'Large', 'S' => 'Small']) }}
  2. {{ BsForm::select('size')->options(['L' => 'Large', 'S' => 'Small']) }}

Generating A Drop-Down List With Selected Default

  1. {{ BsForm::select('size')->options(['L' => 'Large', 'S' => 'Small'])->value('S') }}

Generating a Drop-Down List With an Empty Placeholder

  1. {{ BsForm::select('size')->options(['L' => 'Large', 'S' => 'Small'])->placeholder('Select Size') }}

Generating A Grouped List

  1. {{ BsForm::select('animal',[
  2. 'Cats' => ['leopard' => 'Leopard'],
  3. 'Dogs' => ['spaniel' => 'Spaniel'],
  4. ]) }}

# Generating A Submit Button

  1. {{ BsForm::submit('Click Me!') }}

Generateing A Submit Button With Bootstrap Button Style

  1. {{ BsForm::submit('Click Me!')->success() }}
  2. {{ BsForm::submit('Click Me!')->primary() }}
  3. {{ BsForm::submit('Click Me!')->info() }}
  4. {{ BsForm::submit('Click Me!')->warning() }}
  5. {{ BsForm::submit('Click Me!')->danger() }}

# Supported Methods

->label($label) : To Generate label to the specified field.

  1. {{ BsForm::text('username')->label('Username') }}

->name($name) : To Generate label to the specified field.

  1. {{ BsForm::text('username')->label('Username') }}

->placeholder($placeholder) : To Set placeholder attribute to the specified field.

  1. {{ BsForm::text('username')->placeholder('Please Enter Your Name') }}

->min($min) : To Set min attribute to the specified number field.

  1. {{ BsForm::number('age')->min(10) }}

->max($max) : To Set max attribute to the specified number field.

  1. {{ BsForm::number('age')->min(10)->max(30) }}

->step($step) : To Set step attribute to the specified number field.

  1. {{ BsForm::number('age')->min(10)->max(30)->step(1) }}

->multiple($bool = true) : To Set multiple attribute to the specified select and file fields.

  1. {{ BsForm::file('photos[]')->multiple() }}

->note($note) : To Set help-block to the specified field.

  1. {{ BsForm::text('username')->note('Example: Ahmed Fathy') }}

->name($name) : To Set the name of to the specified field.

  1. {{ BsForm::text()->name('username')->note('Example: Ahmed Fathy') }}

->value($value) : To Set the value of to the specified field as default will set old('name').

  1. {{ BsForm::text()->name('username')->value('Ahmed Fathy') }}

->inlineValidation($bool = true) : To display validation errors in the specified field.

  1. {{ BsForm::text('username')->style('vertical')->inlineValidation(false) }}

->style($style = 'default') : To Set style to the specified field. supported ['default', 'vertical'].

  1. {{ BsForm::text('username')->style('vertical') }}
  2. {{ BsForm::text('email')->style('default') }}

->close() : To close the form tag.

  1. {{ BsForm::close() }}

# Using Resource With Localed Fields

You may add localed labels, notes and placeholders using resource option as well:

  1. @php(BsForm::resource('users'))

You must add users.php file to the resources/lang/en/ path and set the default attributes and notes, placeholders as well:

  1. <?php
  2. return [
  3. 'attributes' => [
  4. 'username' => 'User Name',
  5. 'email' => 'E-mail Address',
  6. 'phone' => 'Phone Number',
  7. ],
  8. 'notes' => [
  9. 'username' => 'Example: Ahmed Fathy',
  10. 'email' => 'Example: aliraqi-dev@gmail.com',
  11. 'phone' => 'Example: +02xxxxxxxxxxx',
  12. ],
  13. 'placeholders' => [
  14. 'username' => 'Please enter your name.',
  15. 'email' => 'Please enter your e-mail address.',
  16. 'phone' => 'Please enter your phone number.',
  17. ],
  18. ...
  19. ];

# Using Custom Error Message Bag

You can using custom error bag to display validation errors without any conflict.

  1. // Default error bag
  2. BsForm::errorBag('default');
  3. // Other error bag
  4. BsForm::errorBag('create');

# Example Register Form

  1. @php(BsForm::resource('users'))
  2. {{ BsForm::post(route('register')) }}
  3. {{ BsForm::text()->name('name') }}
  4. {{ BsForm::email('email') }}
  5. {{ BsForm::text('phone') }}
  6. {{ BsForm::submit()->danger() }}
  7. {{ BsForm::close() }}

# Using Multilingual Form Tabs

  1. {{ BsForm::post(route('categories.store')) }}
  2. @bsMultilangualFormTabs
  3. {{ BsForm::text('name') }}
  4. @endBsMultilangualFormTabs
  5. {{ BsForm::submit()->danger() }}
  6. {{ BsForm::close() }}

Note : the input name inside @bsMultilangualFormTabs and @endBsMultilangualFormTabs suffix with :{lang}.

Ex. if your supported language is ar & en the input will named with name:ar & name:en.

You should use Astrotomic/laravel-translatable and configure it’s rule_factory with key format \Astrotomic\Translatable\Validation\RuleFactory::FORMAT_KEY to fill the multilingual data like the following example.

  1. Category::create([
  2. 'name:ar' => 'سيارات',
  3. 'name:en' => 'Cars',
  4. ]);
  5. // with laravel-bootstrap-forms
  6. Category::create($request->all());

# Manage Locales

You can add your supported locales in locales.php config file. you will get it using the following command:

  1. php artisan vendor:publish --tag=locales:config
  1. <?php
  2. return [
  3. /*
  4. |--------------------------------------------------------------------------
  5. | Application Locales
  6. |--------------------------------------------------------------------------
  7. |
  8. | Contains an array with the applications available locales.
  9. |
  10. */
  11. 'en' => [
  12. 'code' => 'en',
  13. 'name' => 'English',
  14. 'dir' => 'ltr',
  15. 'flag' => '/images/flags/us.png'
  16. ],
  17. 'ar' => [
  18. 'code' => 'ar',
  19. 'name' => 'العربية',
  20. 'dir' => 'rtl',
  21. 'flag' => '/images/flags/sa.png'
  22. ],
  23. ];

# Using Bootstrap 3

If you want to use bootstrap 3 you should publish the config file using the following commad and set the bootstrap version globally.

  1. php artisan vendor:publish --tag=laravel-bootstrap-forms.config
  1. <?php
  2. return [
  3. /**
  4. * The path of form components views.
  5. *
  6. * - 'BsForm::bootstrap4' - Bootstrap 4
  7. * - 'BsForm::bootstrap3' - Bootstrap 3
  8. */
  9. 'views' => 'BsForm::bootstrap3',
  10. ];

After change bootstrap version you should clear the cached view files using the view:clear artisan command.

  1. php artisan view:clear

# Add Custom Style To The Component

run the vendor:publish artusan command to override components views as well.

  1. php artisan vendor:publish --provider="Elnooronline\LaravelBootstrapForms\Providers\BootstrapFormsServiceProvider" --tag BsForm

will override components in resources/views/vendor/BsForm path.

  1. - views
  2. - vendor
  3. - BsForm
  4. - bootstrap4
  5. - text
  6. - default.blade.php
  7. - vertical.blade.php
  8. - custom.blade.php
  9. - email
  10. - default.blade.php
  11. - vertical.blade.php
  12. - custom.blade.php

you can copy default.blade.php file as custom.blade.php and use custom style as well :

  1. {{ BsForm::text('name')->style('custom') }}

you can also set the style globally with BsForm::style() method before the form open as well :

  1. @php(BsForm::style('custom'))

or

  1. @php(BsForm::resource('users')->style('custom'))

To reset the custom style to the default you should call clearStyle() method as well:

  1. @php(BsForm::clearStyle())

For Example :

  1. @php(BsForm::resource('users')->style('web'))
  2. {{ BsForm::model($user, route('users.update', $user)) }}
  3. {{-- All fields here uses web style --}}
  4. {{ BsForm::text('name') }}
  5. {{ BsForm::text('email') }}
  6. @php(BsForm::clearStyle())
  7. {{-- All fields after clearing uses default style --}}
  8. {{ BsForm::text('phone') }}
  9. {{ BsForm::textarea('address') }}
  10. {{ BsForm::submit()->style('inline') }}
  11. {{ BsForm::close() }}

# Add Custom Component

You may add new component class extends BaseComponent and regoster it in your boot() method in AppServiceProvider class as well:

  1. <?php
  2. namespace App\Components;
  3. use Elnooronline\LaravelBootstrapForms\Components\BaseComponent;
  4. class ImageComponent extends BaseComponent
  5. {
  6. /**
  7. * The component view path.
  8. *
  9. * @var string
  10. */
  11. protected $viewPath = 'components.image';
  12. /**
  13. * The image file path.
  14. *
  15. * @var string
  16. */
  17. protected $file;
  18. /**
  19. * Initialized the input arguments.
  20. *
  21. * @param null $name
  22. * @param null $file
  23. * @return $this
  24. */
  25. public function init($name = null, $file = null)
  26. {
  27. $this->name = $name;
  28. $this->value = $file ?: 'http://via.placeholder.com/100x100';
  29. //$this->hasDefaultLocaledLabel($name);
  30. //$this->hasDefaultLocaledNote($name);
  31. //$this->hasDefaultLocaledPlaceholder($name);
  32. return $this;
  33. }
  34. /**
  35. * Set the file path.
  36. *
  37. * @param $file
  38. * @return $this
  39. */
  40. public function file($file)
  41. {
  42. $this->file = $file;
  43. return $this;
  44. }
  45. /**
  46. * The variables with registerd in view component.
  47. *
  48. * @return array
  49. */
  50. protected function viewComposer()
  51. {
  52. return [
  53. 'file' => $this->file,
  54. ];
  55. }
  56. }

Then register the component class in boot() method in your AppServiceProvider class as well :

  1. <?php
  2. namespace App\Providers;
  3. use App\Components\ImageComponent;
  4. use Illuminate\Support\ServiceProvider;
  5. use Elnooronline\LaravelBootstrapForms\Facades\BsForm;
  6. class AppServiceProvider extends ServiceProvider
  7. {
  8. /**
  9. * Bootstrap any application services.
  10. *
  11. * @return void
  12. */
  13. public function boot()
  14. {
  15. //
  16. BsForm::registerComponent('image', ImageComponent::class);
  17. ...
  18. }
  19. ...

Then publish the BsForm views and create the new component file in views/vendor/BsForm/bootstrap4/components/image/default.blade.php path.

Eexample content of views/vendor/BsForm/bootstrap4/components/image/default.blade.php file :

  1. <div class="form-group{{ $errors->has($name) ? ' has-error' : '' }}">
  2. @if($label)
  3. {{ Form::label($name, $label, ['class' => 'content-label']) }}
  4. @endif
  5. {{ Form::file($name, array_merge(['class' => 'form-control'], $attributes)) }}
  6. @if($inlineValidation)
  7. @if($errors->has($name))
  8. <strong class="help-block">{{ $errors->first($name) }}</strong>
  9. @else
  10. <strong class="help-block">{{ $note }}</strong>
  11. @endif
  12. @else
  13. <strong class="help-block">{{ $note }}</strong>
  14. @endif
  15. @if($file)
  16. <div class="row">
  17. <div class="col-xs-6 col-md-3">
  18. <a href="#" class="thumbnail">
  19. <img src="{{ $file }}">
  20. </a>
  21. </div>
  22. </div>
  23. @endif
  24. </div>

Usage

  1. {{ BsForm::image('photo', $url) }}