glhd/aire

Modern Laravel form builder. Remembers old input, retrieves error messages and comes with beautiful Tailwind-based markup out of the box.

0.8.0 2019-04-16 21:35 UTC

README

Build Status Coverage Status Latest Stable Release MIT Licensed

Aire

Aire is a modern Laravel form builder (demo) with a focus on the same expressive and beautiful code you expect from the Laravel ecosystem.

Basic Usage

The most common usage is via the Aire facade in your blade templates. All method calls are fluent, allowing for easy configuration of your form components:

{{ Aire::open()->route('users.update') }}

{{ Aire::bind($user) }}

<div class="flex flex-col md:flex-row">

  {{ Aire::input('given_name', 'First/Given Name')
    ->id('given_name')
    ->autoComplete('off')
    ->groupClass('flex-1 mr-2') }}
    
  {{ Aire::input('family_name', 'Last/Family Name')
    ->id('family_name')
    ->autoComplete('off')
    ->groupClass('flex-1') }}
  
</div>
  
{{ Aire::email('email', 'Email Address') }}
  
{{ Aire::submit('Update User') }}
  
{{ Aire::close() }}

Customization

Aire comes with classes that should work with the default Tailwind class names out of the box (.bg-blue-dark etc). If you need to change the default class names for any given element, there are two different ways to go about it.

The first is to publish the aire.php config file via php artisan vendor:publish --tag=config and update the default_classes config for the element you'd like to change:

return [
  'default_classes' => [
    'input' => 'text-grey-darkest bg-white border rounded-sm',
  ],
];

The second option is to publish custom views via php artisan vendor:publish --tag=views which gives you total control over component rendering. There's a view file for each component type (input.blade.php etc) as well as for component grouping. This gives you the most flexibility, but means that you have the maintain your views as Aire releases add new features or change component rendering.

Configuration

When you publish the aire.php config file via php artisan vendor:publish --tag=config, there are a handful of other configuration options. The config file is fully documented, so go check it out!

Data Binding

Aire automatically binds old input to your form so that values are preserved if a validation error occurs. You can also bind data with the bind() method.

// Bind Eloquent models
Aire::bind(User::find(1));

// Bind an array
Aire::bind(['given_name' => 'Chris']);

// Bind any object
Aire::bind((object) ['given_name' => 'Chris']);

Binding Precedence

Binding is applied in the following order:

  1. Values set with value() are applied no matter what
  2. Old input is applied if available
  3. Bound data is applied last

Method Spoofing & Inference

Aire will automatically add the Laravel _method field for forms that are not GET or POST. It will also automatically infer the intended method from the route if possible.

// In routes
Route::delete('/photos/{photo}', 'PhotosController@destroy')
	->name('photos.destroy');

// In your view
{{ Aire::open()->route('photos.destroy', $photo) }}
{{ Aire::close() }}

Will generate the resulting HTML:

<form action="/photos/1" method="POST">
<input type="hidden" name="_method" value="DELETE" />
</form>

Automatic CSRF Field

Aire will automatically inject a CSRF token if one exists and the form is not a GET form. Simply enable the session and a hidden _token field will be injected for you.

Server-Side Validation

If you run validations on the server, Aire will pick up any errors and automatically apply error classes and show error messages within the associated input's group.

You can also include an error summary, which provides an easy way to show your users an error at the top of the page if validation failed.

// Print "There are X errors on this page that you must fix before continuing."
{{ Aire::summary() }}

// Also include an itemized list of errors
{{ Aire::summary()->verbose() }}

Client-Side Validation

Javascript validation in Aire is currently work in progress. Eventually, Aire will support automatic client-side validation (just call rules() on a field). This will let you set the rules on a field-by-field basis, or for the whole form, and will let you pass in a FormRequest object to automatically inject rules.

Misc. Notes

These notes are here mostly to inform development.

Considerations/Inspiration

Components

  • Button
  • Checkbox
  • Date
  • DateTimeLocal
  • Email
  • File
  • FormControl
  • FormOpen
  • FormClose
  • Hidden
  • Input
  • Label
  • Password
  • RadioButton
  • Select
  • Text
  • TextArea

Input Types

  • button
  • checkbox
  • color
  • date
  • datetime
  • datetime-local
  • email
  • file
  • hidden
  • image
  • month
  • number
  • password
  • radio
  • range
  • reset
  • search
  • submit
  • tel
  • text
  • time
  • url
  • wee