If you need to get started quickly, or just want a basic CRUD form that doesn't have sophisticated design needs, you can use Aire's scaffolding to generate an entire form in seconds:
/**
* DocBlock annotations are used to infer field type
*
* @property string $name
*/
class Author extends Model
{
// Standard Laravel $casts are used to infer field type
protected $casts = [
'is_favorite' => 'bool',
];
// $dates are also used to infer field type
protected $dates = [
'last_read_at',
];
// Or you can add a custom function to configure a field to your heart's content
public function configureGenreFormField(Aire $aire)
{
$genres = [
'cb' => 'Cookbooks',
'dm' => 'Detective and Mystery',
'fa' => 'Fantasy',
'lf' => 'Literary Fiction',
];
return $aire->select($genres, 'genre', 'Genre Best Known For')
->defaultValue('lf');
}
// If you're happy with Aire's inferred field but you need to change the label
// text you can do so without having to give up all the other magic
public function getIsFavoriteFormLabel() : string
{
return 'This is one of my favorite authors';
}
}
{{ Aire::scaffold(Author::class) }}
{{ Aire::scaffold(Author::find(1)) }}
If you need more control or want to build custom form objects that are not
tied to an Eloquent model, you can implement the ConfiguresForm
interface:
class CustomForm implements ConfiguresForm
{
// Use configureForm to set properties on the form object itself
public function configureForm(Form $form, Aire $aire) : void
{
$form->action('/books')
->patch()
->rules([
'title' => 'required|min:5',
'author' => 'required',
'edition' => 'nullable|integer|min:1',
]);
}
public function formFields(Aire $aire) : array
{
return [
'title' => $aire->input('title', 'Title of Book')
->placeholder('Book title'),
'author' => $aire->input('author', 'Author')
->helpText('Please try to be consistent in name formatting'),
'edition' => $aire->number('edition', 'Book Edition')
->min(1)
->step(1)
->defaultValue(1)
->groupAddClass('w-48'),
'submit' => $aire->submit('Save Book'),
];
}
}
{{ Aire::scaffold(new CustomForm())
->bind([
'title' => 'Radical Candor',
'author' => 'Kim Scott',
'edition' => 2,
]) }}