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)
<?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)
{!! $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 --}}
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();
}
}
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)