Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/kristijanhusak/laravel-form-builder

Laravel Form builder for version 5+!
https://github.com/kristijanhusak/laravel-form-builder

form-builder laravel laravel-5-package laravel-framework php

Last synced: 3 months ago
JSON representation

Laravel Form builder for version 5+!

Lists

README

        

[![Build Status](https://travis-ci.org/kristijanhusak/laravel-form-builder.svg)](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

[![Join the chat at https://gitter.im/kristijanhusak/laravel-form-builder](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/kristijanhusak/laravel-form-builder?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

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).

## Bootstrap 4 support
To use bootstrap 4 instead of bootstrap 3, install [laravel-form-builder-bs4](https://github.com/ycs77/laravel-form-builder-bs4).

## Upgrade to 1.6
If you upgraded to `>1.6.*` from `1.5.*` or earlier, and having problems with form value binding, rename `default_value` to `value`.

More info in [changelog](https://github.com/kristijanhusak/laravel-form-builder/blob/master/CHANGELOG.md).

## Documentation
For detailed documentation refer to https://kristijanhusak.github.io/laravel-form-builder/.

## Changelog
Changelog can be found [here](https://github.com/kristijanhusak/laravel-form-builder/blob/master/CHANGELOG.md).

## Installation

### Using Composer

```sh
composer require kris/laravel-form-builder
```

Or manually by modifying `composer.json` file:

``` json
{
"require": {
"kris/laravel-form-builder": "1.*"
}
}
```

And run `composer install`

Then add Service provider to `config/app.php`

``` php
'providers' => [
// ...
Kris\LaravelFormBuilder\FormBuilderServiceProvider::class
]
```

And Facade (also in `config/app.php`)

``` php
'aliases' => [
// ...
'FormBuilder' => Kris\LaravelFormBuilder\Facades\FormBuilder::class
]

```

**Notice**: This package will add `laravelcollective/html` package and load aliases (Form, Html) if they do not exist in the IoC container.

## Quick start

Creating form classes is easy. With a simple artisan command:

```sh
php artisan make:form Forms/SongForm --fields="name:text, lyrics:textarea, publish:checkbox"
```

Form is created in path `app/Forms/SongForm.php` with content:

```php
add('name', Field::TEXT, [
'rules' => 'required|min:5'
])
->add('lyrics', Field::TEXTAREA, [
'rules' => 'max:5000'
])
->add('publish', Field::CHECKBOX);
}
}
```

If you want to instantiate empty form without any fields, just skip passing `--fields` parameter:

```sh
php artisan make:form Forms/PostForm
```

Gives:

```php
create(\App\Forms\SongForm::class, [
'method' => 'POST',
'url' => route('song.store')
]);

return view('song.create', compact('form'));
}

public function store(FormBuilder $formBuilder)
{
$form = $formBuilder->create(\App\Forms\SongForm::class);

if (!$form->isValid()) {
return redirect()->back()->withErrors($form->getErrors())->withInput();
}

// Do saving and other things...
}
}
```

Alternative example:

```php
create(SongForm::class, [
'method' => 'POST',
'url' => route('song.store')
]);

return view('song.create', compact('form'));
}

public function store(FormBuilder $formBuilder)
{
$form = $formBuilder->create(SongForm::class);

if (!$form->isValid()) {
return redirect()->back()->withErrors($form->getErrors())->withInput();
}

// Do saving and other things...
}
}
```

If you want to store a model after a form submit considerating all fields are model properties:

```php
create(\App\Forms\SongForm::class);
$form->redirectIfNotValid();

SongForm::create($form->getFieldValues());

// Do redirecting...
}
```

You can only save properties you need:

```php
create(\App\Forms\SongForm::class);
$form->redirectIfNotValid();

$songForm = new SongForm();
$songForm->fill($request->only(['name', 'artist'])->save();

// Do redirecting...
}
```

Or you can update any model after form submit:

```php
getForm($songForm);
$form->redirectIfNotValid();

$songForm->update($form->getFieldValues());

// Do redirecting...
}
```

Create the routes

```php
// app/Http/routes.php
Route::get('songs/create', [
'uses' => 'SongsController@create',
'as' => 'song.create'
]);

Route::post('songs', [
'uses' => 'SongsController@store',
'as' => 'song.store'
]);
```

Print the form in view with `form()` helper function:

```html

@extends('app')

@section('content')
{!! form($form) !!}
@endsection
```

Go to `/songs/create`; above code will generate this html:

```html



Name



Lyrics



Publish

```

Or you can generate forms easier by using simple array
```php
createByArray([
[
'name' => 'name',
'type' => Field::TEXT,
],
[
'name' => 'lyrics',
'type' => Field::TEXTAREA,
],
[
'name' => 'publish',
'type' => Field::CHECKBOX
],
]
,[
'method' => 'POST',
'url' => route('song.store')
]);

return view('song.create', compact('form'));
}
}
```

## 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`