/home/techb158/immovalet.com/vendor/kris/laravel-form-builder
NameSizeModeActions
.github/-0755rm
src/-0755rm
tests/-0755rm
.gitignore810644editdlrm
.scrutinizer.yml9420644editdlrm
.travis.yml5490644editdlrm
CHANGELOG.md180240644editdlrm
composer.json12420644editdlrm
LICENSE10830644editdlrm
NOTES.md1610644editdlrm
phpunit-printer.yml3260644editdlrm
phpunit.xml10360644editdlrm
README.md96140644editdlrm
README_OLD.md323970644editdlrm
Edit: /home/techb158/immovalet.com/vendor/kris/laravel-form-builder/README_OLD.md (32397B)
[![Build Status](https://img.shields.io/travis/kristijanhusak/laravel-form-builder/master.svg?style=flat)](https://travis-ci.org/kristijanhusak/laravel-form-builder) [![Coverage Status](http://img.shields.io/scrutinizer/coverage/g/kristijanhusak/laravel-form-builder.svg?style=flat)](https://scrutinizer-ci.com/g/kristijanhusak/laravel-form-builder/?branch=master) [![Total Downloads](https://img.shields.io/packagist/dt/kris/laravel-form-builder.svg?style=flat)](https://packagist.org/packages/kris/laravel-form-builder) [![Latest Stable Version](https://img.shields.io/packagist/v/kris/laravel-form-builder.svg?style=flat)](https://packagist.org/packages/kris/laravel-form-builder) [![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](LICENSE) # Laravel 5 form builder Form builder for Laravel 5 inspired by Symfony's form builder. With help of Laravels FormBuilder class creates forms that can be easy modified and reused. By default it supports Bootstrap 3. ## Laravel 4 For laravel 4 version check [laravel4-form-builder](https://github.com/kristijanhusak/laravel4-form-builder) ## Changelog Changelog can be found [here](https://github.com/kristijanhusak/laravel-form-builder/blob/master/CHANGELOG.md) ## Table of contents 1. [Installation](#installation) 2. [Basic usage](#usage) 1. [Usage in controllers](#usage-in-controllers) 2. [Usage in views](#usage-in-views) 3. [Plain form](#plain-form) 4. [Child form](#child-form) 5. [Named form](#named-form) 6. [Collection](#collection) 1. [Collection of child forms](#collection-of-child-forms) 2. [Prototype](#prototype) 7. [Field customization](#field-customization) 8. [Changing configuration and templates](#changing-configuration-and-templates) 9. [Custom fields](#custom-fields) 10. [Contributing](#contributing) 11. [Issues and bug reporting](#issues-and-bug-reporting) ###Installation ``` json { "require": { "kris/laravel-form-builder": "1.5.*" } } ``` run `composer update` Then add Service provider to `config/app.php` ``` php 'providers' => [ // ... 'Kris\LaravelFormBuilder\FormBuilderServiceProvider' ] ``` And Facade (also in `config/app.php`) ``` php 'aliases' => [ // ... 'FormBuilder' => 'Kris\LaravelFormBuilder\Facades\FormBuilder' ] ``` **Notice**: This package will add `illuminate/html` package and load Aliases (Form, Html) if they do not exist in the IoC container ### Basic usage Creating form classes is easy. With a simple artisan command: ``` sh php artisan make:form Forms/PostForm ``` you create form class in path `app/Forms/PostForm.php` that looks like this: ``` php add('name', 'text') ->add('lyrics', 'textarea') ->add('publish', 'checkbox'); } } ``` #### Usage in controllers Forms can be used in controller like this: ``` php create('App\Forms\SongForm', [ 'method' => 'POST', 'url' => route('song.store') ]); return view('song.create', compact('form')); } public function store() { } } ``` #### Usage in views From controller they can be used in views like this: ``` html @extend('layouts.master') @section('content') {!! form($form) !!} @endsection ``` `{!! form($form) !!}` Will generate this html: ``` html
``` There are several helper methods that can help you customize your rendering: ``` html {!! form_row($form->lyrics, ['attr' => ['class' => 'big-textarea']]) !!}
``` You can also split it even more: ``` html {!! form_start($form) !!}
{!! form_label($form->publish) !!} {!! form_widget($form->publish, ['checked' => true]) !!} {!! form_errors($form->publish) !!}
This field is required.
{!! form_rest($form) !!}
{!! form_end($form) !!}
``` ### Plain form If you need to quick create a small form that does not to be reused, you can use `plain` method: ``` php 'POST', 'url' => route('login') ])->add('username', 'text')->add('password', 'password')->add('login', 'submit'); return view('auth.login', compact('form')); } public function postLogin() { } } ``` ### Child form You can add one form as a child in another form. This will render all fields from that child form and wrap them in name provided: ``` php class PostForm { public function buildForm() { $this ->add('title', 'text') ->add('body', 'textarea'); } } class GenderForm { public function buildForm() { $this ->add('gender', 'select', [ 'choices' => $this->getData('genders') ]); } } class SongForm extends Form { public function buildForm() { $this ->add('name', 'text') ->add('gender', 'form', [ 'class' => 'App\Forms\GenderForm', // Passed to gender form as data (same as calling addData($data) method), // works only if class is passed as string 'data' => ['genders' => ['m' => 'Male', 'f' => 'Female']] ]) ->add('song', 'form', [ 'class' => $this->formBuilder->create('App\Forms\PostForm') ]) ->add('lyrics', 'textarea'); } } ``` So now song form will render this: ```html
``` ### Named form Named forms are very similar to child forms, only difference is that they are used as standalone forms. ```php class PostForm { // Can be changed when creating a form protected $name = 'post'; public function buildForm() { $this ->add('title', 'text', [ 'label' => 'Post title' ]) ->add('body', 'textarea', [ 'label' => 'Post body' ]); } } class PostController { public function createAction() { $form = \FormBuilder::create('App\Forms\PostForm'); // Can be set from here in 2 ways: // This allows flexibility to use only when needed // 1. way: $form = \FormBuilder::create('App\Forms\PostForm', [ 'name' => 'post' ]); // 2. way; $form = \FormBuilder::create('App\Forms\PostForm')->setName('post'); } } // View
``` ### Collection Collections are used for working with array of data, mostly used for relationships (OneToMany, ManyToMany). It can be any type that is available in the package. Here are some examples: ``` php add('title', 'text') ->add('body', 'textarea') ->add('tags', 'collection', [ 'type' => 'text', 'property' => 'name', // Which property to use on the tags model for value, defualts to id 'data' => [], // Data is automatically bound from model, here we can override it 'options' => [ // these are options for a single type 'label' => false, 'attr' => ['class' => 'tag'] ] ]); } } ``` And in controller: ```php 1, // 'title' => 'lorem ipsum', // 'body' => 'dolor sit' // 'tags' => [ // ['id' => 1, 'name' => 'work', 'desc' => 'For work'], // ['id' => 2, 'name' => 'personal', 'desc' => 'For personal usage'] // ] // ] // Collection field type will automatically pull tags data from the model, // If we want to override the data, we can pass `data` option to the field $form = $formBuilder->create('App\Forms\PostForm', [ 'model' => $post ]); return view('posts.edit', compact('form')); } } ``` Then the view will contain: ```html
``` #### Collection of child forms [Child form](#child-form) also can be used as a collection. ```php add('name', 'text') ->add('desc', 'textarea'); } } class PostForm extends Form { public function buildForm() { $this ->add('title', 'text') ->add('body', 'textarea') ->add('tags', 'collection', [ 'type' => 'form', 'options' => [ // these are options for a single type 'class' => 'App\Forms\TagsForm' 'label' => false, ] ]); } } ``` And with same controller setup as above, we get this: ```html
``` #### Prototype If you need to dynamically generate HTML for additional elements in the collection, you can use `prototype()` method on the form field. Let's use example above: ```html @extends('app') @section('content') {!! form_start($form) !!}
// Use {{ }} here to escape html {!! form_row($form->tags) !!}
{!! form_end($form) !!} @endsection ``` `data-prototype` will contain: ```html
``` And clicking on the button `.add-to-collection` will automatically generate proper html from the prototype. Prototype can be configured in the form class: ```php use Kris\LaravelFormBuilder\Form; class PostForm extends Form { public function buildForm() { $this ->add('title', 'text') ->add('body', 'textarea') ->add('tags', 'collection', [ 'type' => 'text', 'property' => 'name', 'prototype' => true, // Should prototype be generated. Default: true 'prototype_name' => '__NAME__' // Value used for replacing when generating new elements from prototype, default: __NAME__ 'options' => [ 'label' => false, 'attr' => ['class' => 'tag'] ] ]); } } ``` ### Field Customization Fields can be easily customized within the class or view: ``` php add('name', 'text', [ 'wrapper' => [ 'class' => 'name-input-container' ], 'required' => true, // Adds `required` class to label, and required attribute to field 'help_block' => [ 'text' => 'I am help text', // If text is set, automatically adds help text under the field. Default: null 'tag' => 'p' // this is default, 'attr' => ['class' => 'help-block'] // Default, class pulled from config file ] 'attr' => ['class' => 'input-name', 'placeholder' => 'Enter name here...'], 'label' => 'Full name' ]) ->add('bio', 'textarea', [ 'wrapper' => false // This disables the wrapper for this field ]) // This creates a select field ->add('subscription', 'choice', [ 'choices' => ['monthly' => 'Monthly', 'yearly' => 'Yearly'], 'empty_value' => '==== Select subscription ===', 'multiple' => false // This is default. If set to true, it creates select with multiple select posibility ]) ->add('categories', 'entity', [ 'class' => 'App\Category', // Entity that holds data 'property' => 'name', // Value that will be used as a label for each choice option, default: name 'property_key' => 'id', // Value that will be used as a value for each choice option, default: id 'query_builder' => function(App\Category $category) { // If provided, gets data from this closure and lists it return $category->where('active', 1); } ]) // This creates radio buttons ->add('gender', 'choice', [ 'label' => false, // This forces hiding label, even when calling form_row 'choices' => ['m' => 'Male', 'f' => 'Female'], 'selected' => 'm', 'expanded' => true, 'choice_options' => [ // Handles options when expanded is true and/or multiple is true 'wrapper' => ['class' => 'choice-wrapper'] // Shows the wrapper for each radio or checkbox, default is false ] ]) // Static text, holds only text, no input ->add('address', 'static', [ 'tag' => 'div' // Tag to be used for holding static data, 'attr' => ['class' => 'form-control-static'], // This is the default 'default_value' => null // If nothing is passed, data is pulled from model if any ]) // Automatically adds enctype="multipart/form-data" to form ->add('image', 'file', [ 'label' => 'Upload your image' ]) // This creates a checkbox list ->add('languages', 'choice', [ 'choices' => [['id' => 1, 'en' => 'English'], ['id' => 2, 'de' => 'German'], ['id' => 3, 'fr' => 'France']], 'selected' => function ($data) { // Allows handling data before passed to view for setting default values. Useful for related models return array_pluck($data, 'id'); } 'expanded' => true, 'multiple' => true ]) // Renders all fieds from song form and wraps names for better handling // becomes ->add('song', 'form', [ 'class' => $this->formBuilder->create('App\Forms\SongForm') ]) ->add('policy-agree', 'checkbox', [ 'default_value' => 1, // 'label' => 'I agree to policy', 'checked' => false // This is the default. ]) // Creates 2 inputs. These are the defaults ->add('password', 'repeated', [ 'type' => 'password' // can be anything that fits 'second_name' => 'password_confirmation', // defaults to name_confirmation 'first_options' => [], // Same options available as for text type 'second_options' => [], // Same options available as for text type ]) ->add('save', 'submit', [ 'attr' => ['class' => 'btn btn-primary'] ]) ->add('clear', 'reset', [ 'label' => 'Clear the form', 'attr' => ['class' => 'btn btn-danger'] ]); } } ``` You can also remove fields from the form when neccessary. For example you don't want to show `clear` button and `subscription` fields on the example above on edit page: ``` php 'PUT', 'url' => route('posts.update', $id), 'model' => $post ]) ->remove('clear') ->remove('subscription'); return view('posts.edit', compact('form')); } public function update($id) { } } ``` Or you can modify it in the similar way (options passed will be merged with options from old field, if you want to overwrite it pass 4th parameter as `true`) ``` php // ... public function edit($id) { $post = Post::findOrFail($id); $form = \FormBuilder::create(PostForm::class, [ 'method' => 'PUT', 'url' => route('posts.update', $id), 'model' => $post, ]) // If passed name does not exist, add() method will be called with provided params ->modify('gender', 'select', [ 'attr' => ['class' => 'form-select'] ], false) // If this is set to true, options will be overwritten - default: false return view('posts.edit', compact('form')); } ``` In a case when `choice` type has `expanded` set to `true` and/or `multiple` also set to true, you get a list of radios/checkboxes: ``` html
``` If you maybe want to customize how each radio/checkbox is rendered, maybe wrap it in some container, you can loop over children on `languages` choice field: ``` php // ... languages->getChildren() as $child): ?>
true]) ?>
// ... ``` Here is a categorized list of all available field types: * Simple * text * textarea * select * choice * checkbox * radio * password * hidden * file * static * Date and Time * date * datetime-local * month * time * week * Special Purpose * color * search * image * email * url * tel * number * range * Buttons * submit * reset * button * Form Builder Extensions * repeated * [form](#child-form) * [collection](#collection) You can also bind the model to the class and add other options with setters ``` php setMethod('PUT') ->setUrl(route('post.update')) ->setModel($model) // This will automatically do Form::model($model) in the form ->setData('post_choices', [ 'y' => 'yes', 'n' => 'no']) // This can be used in form like $this->getData('post_choices') ->addData([ // Add multiple data values at once 'name' => 'some_name', 'some_other_data' => 'some other data' ]); // Code above is similar to this: $form = \FormBuilder::create('App\Forms\PostForm', [ 'method' => 'PUT', 'url' => route('post.update'), 'model' => $model, 'data' => [ 'post_choices' => [ 'y' => 'yes', 'n' => 'no'] ] ]); or this: $form = \FormBuilder::create('App\Forms\PostForm')->setFormOptions([ 'method' => 'PUT', 'url' => route('post.update'), 'model' => $model, 'data' => [ 'post_choices' => [ 'y' => 'yes', 'n' => 'no'] ] ]); // Any options passed like this except 'model' and 'data' will be passed to the view for form options // So if you need to pass any data to form class, and use it only there, use setData() method or 'data' key // and pass what you need return view('posts.edit', compact('form')); } public function update() { } } ``` And in form, you can use that model to populate some fields like this ``` php getRequest()->all(); $this ->add('title', 'text') ->add('body', 'textearea') ->add('some_choices', 'choices', [ 'choices' => $this->getData('post_choices') // When form is created passed as ->setData('post_choices', ['some' => 'array']) ]) ->add('category', 'select', [ 'choices' => $this->model->categories()->lists('id', 'name') ]); } } ``` ### Changing configuration and templates As mentioned above, bootstrap 3 form classes are used. If you want to change the defaults you can override it by running ```sh php artisan vendor:publish ``` This will create config file `config/laravel-form-builder.php` and folder with views in `resources/views/vendor/laravel-form-builder`. Structure of the config needs to be like this: [config.php](https://github.com/kristijanhusak/laravel-form-builder/blob/master/src/config/config.php) file. change values in `defaults` key as you wish. If you would like to avoid typing in full namespace of the form class when creating, you can add default namespace to the config that was just published, and it will prepend it every time you want to create form: ``` php 'App\Forms' ] // app/Http/Controllers/HomeController public function indexAction() { \FormBuilder::create('SongForm'); } ``` It is empty by default. All views for fields and forms needs to be similar to this: [views](https://github.com/kristijanhusak/laravel-form-builder/tree/master/src/views) Other way is to change path to the templates in the [config.php](https://github.com/kristijanhusak/laravel-form-builder/blob/master/src/config/config.php) file. ``` php return [ // ... 'checkbox' => 'posts.my-custom-checkbox' // resources/views/posts/my-custom-checkbox.blade.php ]; ``` One more way to change template is directly from Form class: ``` php add('title', 'text') ->add('body', 'textearea', [ 'template' => 'posts.textarea' // resources/views/posts/textarea.blade.php ]); } } ``` **When you are adding custom templates make sure they inherit functionality from defaults to prevent breaking.** ### Custom fields If you want to create your own custom field, you can do it like this: ``` php // ... ``` **Notice:** Package templates uses plain PHP for printing because of plans for supporting version 4 (prevent conflict with tags), but you can use blade for custom fields, just make sure to use tags that are not escaping html (`{!! !!}`) And then add it to published config file(`config/packages/kris/laravel-form-builder/config.php`) in key `custom-fields` key this: ``` php // ... 'custom_fields' => [ 'datetime' => 'App\Forms\Fields\DatetimeType' ] // ... ``` Or if you want to load it only for a single form, you can do it directly in BuildForm method: ``` php addCustomField('datetime', 'App\Forms\Fields\DatetimeType'); $this ->add('title', 'text') ->add('created_at', 'datetime') } } ``` ### Contributing Project follows [PSR-2](http://www.php-fig.org/psr/psr-2/) standard and it's covered with PHPUnit tests. Pull requests should include tests and pass [Travis CI](https://travis-ci.org/kristijanhusak/laravel-form-builder) build. To run tests first install dependencies with `composer install`. After that tests can be run with `vendor/bin/phpunit` ### Todo * Add possibility to disable showing validation errors under fields - **DONE** * Add event dispatcher ?