2017 © Pedro Peláez
 

library laravel-form-builder

Laravel form builder - symfony like

image

kris/laravel-form-builder

Laravel form builder - symfony like

  • Friday, July 27, 2018
  • by kristijanhusak
  • Repository
  • 62 Watchers
  • 975 Stars
  • 205,730 Installations
  • PHP
  • 35 Dependents
  • 1 Suggesters
  • 192 Forks
  • 82 Open issues
  • 58 Versions
  • 11 % Grown

The README.md

Build Status Coverage Status Total Downloads Latest Stable Version License, (*1)

Laravel 5 form builder

Join the chat at https://gitter.im/kristijanhusak/laravel-form-builder, (*2)

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., (*3)

Laravel 4

For Laravel 4 version check laravel4-form-builder., (*4)

Bootstrap 4 support

To use bootstrap 4 instead of bootstrap 3, install laravel-form-builder-bs4., (*5)

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., (*6)

More info in changelog., (*7)

Documentation

For detailed documentation refer to https://kristijanhusak.github.io/laravel-form-builder/., (*8)

Changelog

Changelog can be found here., (*9)

Installation

Using Composer

composer require kris/laravel-form-builder

Or manually by modifying composer.json file:, (*10)

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


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), (*12)

``` php 'aliases' => [ // ... 'FormBuilder' => Kris\LaravelFormBuilder\Facades\FormBuilder::class ], (*13)


**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:, (*14)

<?php

namespace App\Forms;

use Kris\LaravelFormBuilder\Form;
use Kris\LaravelFormBuilder\Field;

class SongForm extends Form
{
    public function buildForm()
    {
        $this
            ->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:, (*15)

php artisan make:form Forms/PostForm

Gives:, (*16)

<?php

namespace App\Forms;

use Kris\LaravelFormBuilder\Form;

class PostForm extends Form
{
    public function buildForm()
    {
        // Add fields here...
    }
}

After that instantiate the class in the controller and pass it to view:, (*17)

<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use Kris\LaravelFormBuilder\FormBuilder;

class SongsController extends BaseController {

    public function create(FormBuilder $formBuilder)
    {
        $form = $formBuilder->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:, (*18)

<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use Kris\LaravelFormBuilder\FormBuilder;
use App\Forms\SongForm;

class SongsController extends BaseController {

    public function create(FormBuilder $formBuilder)
    {
        $form = $formBuilder->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:, (*19)

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Kris\LaravelFormBuilder\FormBuilder;
use App\SongForm;

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

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

        // Do redirecting...
    }

You can only save properties you need:, (*20)

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Kris\LaravelFormBuilder\FormBuilder;
use App\SongForm;

class SongFormController extends Controller
{
    public function store(FormBuilder $formBuilder, Request $request)
    {
        $form = $formBuilder->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:, (*21)

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Kris\LaravelFormBuilder\FormBuilder;
use App\SongForm;

class SongFormController extends Controller
{
    public function update(int $id, Request $request)
    {
        $songForm = SongForm::findOrFail($id);

        $form = $this->getForm($songForm);
        $form->redirectIfNotValid();

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

        // Do redirecting...
    }

Create the routes, (*22)

// 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:, (*23)



@extends('app')

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

Go to /songs/create; above code will generate this html:, (*24)

<form method="POST" action="http://example.dev/songs">
    <input name="_token" type="hidden" value="FaHZmwcnaOeaJzVdyp4Ml8B6l1N1DLUDsZmsjRFL">
    <div class="form-group">
        <label for="name" class="control-label">Name</label>
        <input type="text" class="form-control" id="name">
    </div>
    <div class="form-group">
        <label for="lyrics" class="control-label">Lyrics</label>
        <textarea name="lyrics" class="form-control" id="lyrics"></textarea>
    </div>
    <div class="form-group">
        <label for="publish" class="control-label">Publish</label>
        <input type="checkbox" name="publish" id="publish">
    </div>
</form>

Or you can generate forms easier by using simple array, (*25)

<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use Kris\LaravelFormBuilder\FormBuilder;
use Kris\LaravelFormBuilder\Field;
use App\Forms\SongForm;

class SongsController extends BaseController {

    public function create(FormBuilder $formBuilder)
    {
        $form = $formBuilder->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 standard and it's covered with PHPUnit tests. Pull requests should include tests and pass Travis CI build., (*26)

To run tests first install dependencies with composer install., (*27)

After that tests can be run with vendor/bin/phpunit, (*28)

The Versions

27/07 2018

dev-master

9999999-dev

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

laravel form builder

11/07 2018

1.15.1

1.15.1.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

04/07 2018

1.15.0

1.15.0.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

17/12 2017

1.14.0

1.14.0.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

19/09 2017

1.13.0

1.13.0.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

29/05 2017

1.12.1

1.12.1.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

28/05 2017

1.12.0

1.12.0.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

31/01 2017

1.11.0

1.11.0.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

20/12 2016

1.10.0

1.10.0.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

08/09 2016

1.9.0

1.9.0.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

12/08 2016

1.8.0

1.8.0.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

03/06 2016

1.7.10

1.7.10.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

31/05 2016

1.7.0

1.7.0.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

22/01 2016

1.6.50

1.6.50.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

08/01 2016

1.6.42

1.6.42.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

04/01 2016

1.6.41

1.6.41.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

28/12 2015

1.6.40

1.6.40.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

16/11 2015

1.6.31

1.6.31.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

22/09 2015

1.6.30

1.6.30.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

06/08 2015

1.6.20

1.6.20.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

13/07 2015

1.6.12

1.6.12.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

30/06 2015

1.6.11

1.6.11.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

22/06 2015

1.6.10

1.6.10.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

12/06 2015

1.6.0

1.6.0.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

27/05 2015

1.5.10

1.5.10.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

22/05 2015

1.5.1

1.5.1.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

22/05 2015

1.5.0

1.5.0.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

14/05 2015

1.4.26

1.4.26.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

05/05 2015

1.4.25

1.4.25.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

30/04 2015

1.4.22

1.4.22.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

29/04 2015

1.4.21

1.4.21.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

22/04 2015

1.4.20

1.4.20.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

20/04 2015

1.4.13

1.4.13.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

19/04 2015

1.4.12

1.4.12.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

18/04 2015

1.4.11

1.4.11.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

18/04 2015

1.4.10

1.4.10.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

15/04 2015

1.4.06

1.4.06.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

15/04 2015

1.4.05

1.4.05.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

15/04 2015

1.4.04

1.4.04.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

02/04 2015

1.4.03

1.4.03.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

31/03 2015

1.4.02

1.4.02.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

27/03 2015

1.4.01

1.4.01.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

26/03 2015

1.4.0

1.4.0.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

23/03 2015

1.3.80

1.3.80.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

20/03 2015

1.3.71

1.3.71.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

19/03 2015

1.3.7

1.3.7.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

12/03 2015

1.3.6

1.3.6.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

06/03 2015

1.3.5

1.3.5.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

06/03 2015

1.3.4

1.3.4.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

28/02 2015

1.3.3

1.3.3.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

26/02 2015

1.3.2

1.3.2.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

25/02 2015

1.3.1

1.3.1.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

24/02 2015

1.3.0

1.3.0.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

13/02 2015

1.2.0

1.2.0.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

16/01 2015

0.1.8

0.1.8.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

19/10 2014

1.1.1

1.1.1.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

19/10 2014

1.1

1.1.0.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak

18/10 2014

1.0

1.0.0.0

Laravel form builder - symfony like

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristijan Husak