Most applications have many forms. Each form has labels, help texts, required fields and validation rules. In a Blade view, you write the same markup again for each field. When a form changes, you must change the view and the validation rules, and they can easily get out of sync.

We wanted to define a form one time, in PHP, and keep the design in our own Blade components. For this, we made Laravel Form Architect.

What the package does

  • It gives PHP classes for a form, a fieldset and the common fields.
  • It shows each field with a Blade component that you write and control.
  • It collects the validation rules from the fields.
  • It connects the fields to Livewire properties.
  • It makes select options from an enum.

The package does not include a design. You keep your own Blade components, so the forms look like the rest of your application.

Install the package

composer require bernskioldmedia/laravel-form-architect

Then publish the config file:

php artisan vendor:publish --tag="laravel-form-architect-config"

Connect your Blade components

The config file tells the package which Blade component to use for each part of a form. By default, it expects components in a form folder, such as form.input and form.select:

'components' => [
    'form' => 'form.index',
    'fieldset' => 'form.fieldset',
    'field' => 'form.field',
    'input' => 'form.input',
    'checkbox' => 'form.checkbox',
    'radio' => 'form.radio',
    'select' => 'form.select',
    'textarea' => 'form.textarea',
    'multiselect' => 'form.multiselect',
],

The package gives each component the data of the field as attributes. For example, an input gets the name, the label, the value, the type, the help text and the required state. The field component is a wrapper around each field. Use it for the layout, for example for the column width.

Define a form

Make a form with fields and fieldsets:

use BernskioldMedia\LaravelFormArchitect\Forms\Fields\Input;
use BernskioldMedia\LaravelFormArchitect\Forms\Fields\Select;
use BernskioldMedia\LaravelFormArchitect\Forms\Fields\Textarea;
use BernskioldMedia\LaravelFormArchitect\Forms\Fieldset;
use BernskioldMedia\LaravelFormArchitect\Forms\Form;

$form = Form::make([
    Fieldset::make('Contact', [
        Input::make('name', 'Name')
            ->required()
            ->rules('required', 'string', 'max:255'),

        Input::make('email', 'Email')
            ->email()
            ->required()
            ->rules('required', 'email'),
    ])->columns(2),

    Select::make('topic', 'Topic')
        ->attachEnum(Topic::class)
        ->rules('required'),

    Textarea::make('message', 'Message')
        ->rows(6)
        ->helpText('Tell us a little about your project.')
        ->fullwidth(),
]);

Show the form

Give the form to the Blade component of the package:

<x-form-architect::form :form="$form" />

The package goes through the fields. For each field, it uses the wrapper component and the component of the field type. For a fieldset, it uses the fieldset component and then shows the fields in it.

Use the form with Livewire

Call wired() on a field to connect it to a Livewire property with the same name. To use a different property, give its name. Call live() to update the property each time the value changes.

Input::make('email', 'Email')->wired('form.email')->live();

Your Blade component gets the property name, so it can add wire:model to the input.

To validate the form in Livewire, get the rules from the form. The keys are the Livewire property names:

public function rules(): array
{
    return $this->form()->getRules();
}

Options from an enum

A select field can get its options from an enum. Call attachEnum() with the enum class. The enum must have a static asSelectArray() method that returns the options.

A limit to know

The package does not include the Blade components for the fields. You must make them before you can show a form. This is more work at the start, but then the forms use your design and your markup. The radio field does not support options yet, so for a group of options, use a select field.

Get the package

Laravel Form Architect is open source, with the MIT license. The code is on GitHub. If you find a problem or have an idea, open an issue. We are happy to hear from you.