2017 © Pedro Peláez
 

library laravel-form-presenter

Laravel Form Presenter

image

smallhadroncollider/laravel-form-presenter

Laravel Form Presenter

  • Monday, June 5, 2017
  • by smallhadroncollider
  • Repository
  • 1 Watchers
  • 1 Stars
  • 183 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 35 Versions
  • 0 % Grown

The README.md

Laravel Form Presenter

A Laravel package for easily generating and manipulating forms., (*1)

Dealing with forms in plain HTML leads to a horrible mess. Using the Laravel Collective HTML & Forms package makes things neater, but the logic for your form is still all over the place. Using Laravel Form Presenter you can define your form in one place and display it using a reusable form template., (*2)


Features

  • Easy to add your own field types
  • Adds CSRF field automatically
  • Adds required attributes on required fields
  • (Almost) Automatic integration testing
  • Automatically detects file field types and adds the necessary form attributes

Installation

Add SmallHadronCollider\LaravelFormPresenter\Providers\FormPresenterServiceProvider::class to your service providers in config/app.php., (*3)


Example

<?php

// app/Http/Presenters/PersonFieldSet.php

namespace App\Http\Presenters;

use SmallHadronCollider\LaravelFormPresenter\FieldSetPresenter;

class PersonFieldSet extends FieldSetPresenter
{
    protected function fields()
    {
        return [
            [
                "name" => "first_name",
                "type" => "text",
                "label" => "First Name",
            ],
            [
                "name" => "last_name",
                "type" => "text",
                "label" => "Last Name",
                "rules" => ["required"],
            ],
            [
                "name" => "age",
                "type" => "number",
                "label" => "Age",
                "rules" => ["required"],
            ],
            [
                "name" => "notes",
                "type" => "textarea",
                "label" => "Notes",
                "attrs" => ["class" => "js__rich-text"],
            ],
        ];
    }
}
<?php

// app/Http/Controllers/PersonController.php

namespace App\Http\Controllers;

use App\Http\Presenters\PersonFieldSet;
use App\Http\Requests\PersonFormRequest;
use App\Repositories\PersonRepository;

class PersonController extends Controller
{
    private $repository;

    public function __construct(PersonRepository $repository)
    {
        $this->repository = $repository;
    }

    public function get(PersonFieldSet $fieldSet, $id)
    {
        $person = $this->repository->find($id);

        // Set the form data from the $person entity
        $fieldSet->setModel($person);

        return view("form", [
            // Pass in the form for the fieldset
            "form" => $fieldSet->form(),
            "title" => "Update Person"
        ]);
    }

    public function post(PersonFieldSet $fieldSet, PersonFormRequest $request, $id)
    {
        $person = $this->repository->find($id);

        // Get the data for each form field
        $data = $request->only($fieldSet->fieldNames());
        $this->repository->update($person, $data);

        return redirect("/people/{$person->id}");
    }
}

Get the validation rules from the fieldset:, (*4)

<?php

// app/Http/Request/PersonFormRequest.php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Http\Presenters\FieldSets\PersonFieldSet;

class PersonFormRequest extends FormRequest
{
    public function rules(PersonFieldSet $fieldset)
    {
        return $fieldset->rules();
    }
}

You can then use a single form.blade.php file to render all your forms:, (*5)

{{-- form.blade.php --}}

{!! $form->open(["class" => "form"]) !!}
    {!! $form->display() !!}
    {!! $form->submit($title, ["class" => "button button--blue"]) !!}
{!! $form->close() !!}

Or, if you want more flexibility, you can render individual fields in fieldset specific templates:, (*6)

{{-- person-form.blade.php --}}

{!! $form->open(["class" => "form"]) !!}
    {!! $form->first_name !!}
    {!! $form->last_name !!}
    {!! $form->submit($title, ["class" => "button button--blue"]) !!}
{!! $form->close() !!}

Usage

Custom Field Rendering

You can use render a field however you like:, (*7)

{{-- forms/field.blade.php --}}



{!! $field->label(["class" => "form__label"]) !!} @if ($errors->has($field->name())) {{ $errors->first($field->name()) }} @endif {!! $field->display(["class" => "form__control form__control--grouped form__control--{$field->type()}"]) !!}
<?php

// app/Providers/AppServiceProvider.php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use SmallHadronCollider\LaravelFormPresenter\FieldPresenter;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        FieldPresenter::presenter(function ($attrs) {
            return view("forms/field", $attrs);
        });
    }
}

Custom Field Properties

<?php

// app/Http/Presenters/PersonFieldSet.php

namespace App\Http\Presenters;

use SmallHadronCollider\LaravelFormPresenter\FieldSetPresenter;

class PersonFieldSet extends FieldSetPresenter
{
    protected function fields()
    {
        return [
            [
                "name" => "first_name",
                "type" => "text",
                "label" => "First Name",

                // Custom field property
                "message" => "Enter you first name"
            ],
        ];
    }
}

Reserved property names: name, type, label, value, rules, items, attrs, (*8)

Using a custom field renderer as above:, (*9)

{{-- forms/field.blade.php --}}



{!! $field->label(["class" => "form__label"]) !!} @if ($errors->has($field->name())) {{ $errors->first($field->name()) }} @endif {{-- use custom message field - will return null if no value set --}}

{!! $field->message() !!}, (*10)

{!! $field->display(["class" => "form__control form__control--grouped form__control--{$field->type()}"]) !!}

Bootstrap Field Rendering

You can use the built-in Bootstrap template:, (*11)

<?php

// app/Providers/AppServiceProvider.php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use SmallHadronCollider\LaravelFormPresenter\FieldPresenter;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        FieldPresenter::presenter(function ($attrs) {
            return view("form-presenter::bootstrap", $attrs);
        });
    }
}

Adding A Custom Field Type

You can easily add your own custom field type by extending the Field class:, (*12)

<?php

// app/Http/Presenters/Forms/Fields/Boolean.php

namespace App\Http\Presenters\Forms\Fields;

use Illuminate\Foundation\Testing\TestCase;
use Faker\Generator;

use SmallHadronCollider\LaravelFormPresenter\Fields\AbstractField;
use SmallHadronCollider\LaravelFormPresenter\Fields\FieldInterface;

class Boolean extends AbstractField implements FieldInterface
{
    // The html to display for the label
    public function label($attrs = [])
    {
        return $this->formBuilder->label($this->name, $this->label, $attrs);
    }

    // The html to display for the field
    public function display($attrs = [])
    {
        return view("embellish::forms/boolean", [
            "name" => $this->name,
            "value" => $this->value() || $this->value() === null,
        ]);
    }

    // Test function for integration testing
    public function test(TestCase $test, Generator $faker)
    {
        return mt_rand(0, 1) ? $test->check($this->name) : $test->uncheck($this->name);
    }
}
{{-- forms/boolean.blade.php --}}



<?php

// app/Providers/AppServiceProvider

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use SmallHadronCollider\LaravelFormPresenter\FieldPresenter;
use App\Http\Presenters\Forms\Fields\Boolean;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        // Add boolean field type
        FieldPresenter::add("boolean", Boolean::class);
    }
}

Integration Testing

You can setup integration tests for any fieldset automatically:, (*13)

<?php

// tests\AddPersonTest.php

use TestCase;
use SmallHadronCollider\LaravelFormPresenter\PubliciseFormMethods;
use App\Http\Presenters\FieldSets\PersonFieldSet;

class AddPersonTest extends TestCase
{
    // puts form filling methods (type(), select(), etc.) into public scope using __call()
    use PubliciseFormMethods;

    public function testPersonForm()
    {
        // create fieldset
        $fieldset = new PersonFieldSet();

        // visit page
        $test = $this->visit("/admin/people/create");

        // populate test form automatically, then submit
        $fieldset->populateTest($test)->press("Create Person")->assertResponseOk();
    }
}

To Do

  • Better documentation (nested fieldset, multifields)
  • Add some example files
  • Multiple fieldsets per form
  • Custom fieldset rendering
  • One dimensional multifields (e.g. name[0], name[1], name[2])

License

The MIT License (MIT) Copyright © 2016 Small Hadron Collider / Mark Wales, (*14)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:, (*15)

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software., (*16)

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE., (*17)

The Versions

05/06 2017

dev-develop

dev-develop

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

05/06 2017

dev-master

9999999-dev

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

05/06 2017

0.6.17

0.6.17.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

01/03 2017

0.6.16

0.6.16.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

01/03 2017

0.6.15

0.6.15.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

10/10 2016

0.6.14

0.6.14.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

30/09 2016

0.6.13

0.6.13.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

30/09 2016

0.6.12

0.6.12.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

30/09 2016

0.6.11

0.6.11.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

11/07 2016

0.6.10

0.6.10.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

08/06 2016

0.6.9

0.6.9.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

11/05 2016

0.6.8

0.6.8.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

05/05 2016

0.6.7

0.6.7.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

05/05 2016

0.6.6

0.6.6.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

05/05 2016

dev-hotfix/0.6.6

dev-hotfix/0.6.6

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

05/05 2016

0.6.5

0.6.5.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

28/04 2016

0.6.0

0.6.0.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

28/04 2016

0.5.7

0.5.7.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

28/04 2016

0.5.6

0.5.6.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

28/04 2016

0.5.5

0.5.5.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

28/04 2016

0.5.4

0.5.4.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

27/04 2016

0.5.3

0.5.3.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

27/04 2016

0.5.2

0.5.2.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

26/04 2016

0.5.1

0.5.1.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

26/04 2016

0.5.0

0.5.0.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

26/04 2016

0.4.0

0.4.0.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

26/04 2016

0.3.0

0.3.0.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

25/04 2016

0.2.7

0.2.7.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

25/04 2016

0.2.5

0.2.5.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

25/04 2016

0.2.4

0.2.4.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

15/04 2016

0.2.3

0.2.3.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

15/04 2016

0.2.2

0.2.2.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

15/04 2016

0.2.1

0.2.1.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

14/04 2016

0.2.0

0.2.0.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter

14/04 2016

0.1.0

0.1.0.0

Laravel Form Presenter

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel forms presenter