[](https://travis-ci.org/kristijanhusak/laravel-form-builder)
[](https://scrutinizer-ci.com/g/kristijanhusak/laravel-form-builder/?branch=master)
[](https://packagist.org/packages/kris/laravel-form-builder)
[](https://packagist.org/packages/kris/laravel-form-builder)
[](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) !!}
```
### 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