Laravel Livewire form component with declarative Bootstrap 5 fields and buttons.

Overview

Laravel Livewire Forms

Laravel Livewire form component with declarative Bootstrap 5 fields and buttons.

Requirements

  • Bootstrap 5

Installation

composer require bastinald/laravel-livewire-forms

Usage

Make a new form:

php artisan make:form users.create-user-form

Declare fields:

public function fields()
{
    return [
        Input::make('name', 'Name'),
        Input::make('email', 'Email')->type('email'),
        Input::make('password', 'Password')->type('password'),
    ];
}

Declare buttons:

public function buttons()
{
    return [
        Button::make('Create User')->click('createUser'),
        Button::make('Cancel', 'secondary')->url('/'),
    ];
}

Declare rules:

public function rules()
{
    return [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8'],
    ];
}

Declare an action (notice the use of ->click('createUser') in the button example above:

public function createUser()
{
    $this->validate();

    User::create([
        'name' => $this->data('name'),
        'email' => $this->data('email'),
        'password' => Hash::make($this->data('password')),
    ]);

    return redirect('/');
}

Full Page Forms

Create a full page form by specifying a title, layout and route to use:

class Login extends FormComponent
{
    public $title = 'Login';
    public $layout = 'layouts.card';

    public function route()
    {
        return Route::get('/login', static::class)
            ->name('login')
            ->middleware('guest');
    }

The route method is made available by using my laravel-livewire-routes package.

Setting Initial Data

You can set the initial form data / defaults via the data array property in your component mount method:

class UpdateUserForm extends FormComponent
{
    public $title = 'Update User';
    
    public function route()
    {
        return Route::get('/users/update/{user}', static::class)
            ->name('users.update')
            ->middleware('auth');
    }
    
    public function mount(User $user)
    {
        $this->data = $user->toArray();
    }

Accessing Data

Use the data method in the component to access current form data (you can use dot notation for array values):

public function createUser()
{
    User::create([
        'name' => $this->data('name'),
        'email' => $this->data('email'),
        'likes_turtles' => $this->data('answers.likes_turtles'),
    ]);
}

Data Binding

Most fields allow you to change the way livewire binds data via helper methods that are chained to fields e.g.:

Input::make('name', 'Name'), // defaults to defer
Input::make('name', 'Name')->instant(), // bind on keyup 
Input::make('name', 'Name')->defer(), // bind on action 
Input::make('name', 'Name')->lazy(), // bind on change
Input::make('name', 'Name')->debounce(500), // bind after 500ms delay 

Sizing

Many fields also allow you to specify a size for the input e.g.:

Input::make('name', 'Name'), // defaults to normal sizing
Input::make('name', 'Name')->small(), // small sizing
Input::make('name', 'Name')->large(), // large sizing

Disabling

Some fields allow you to disable or set them to readonly, and even plaintext for inputs:

Input::make('name', 'Name')->disabled(),
Input::make('name', 'Name')->readonly(),
Input::make('name', 'Name')->plaintext(),

Helper Text

Specify helper text for a field by using the help method:

Input::make('name', 'Name')->help('Please tell us your name!'),

Available Fields

Alert ($message, $style = 'success')

An alert box.

Alert::make('It worked!'),
Alert::make('Something bad happened.', 'danger'),
Alert::make('Something else happened.')->dismissible(),

The $style parameter accepts a bootstrap alert style e.g. success, danger, primary, etc. Use the dismissible method to make the alert dismissible.

Available methods: dismissible

Arrayable ($name, $label = null)

An array of fields.

Arrayable::make('locations', 'Locations')->fields([
    Input::make('city')->placeholder('City'),
    Select::make('state')->placeholder('State')->options(['FL', 'TX']),
]),

Available methods: fields, help, disabled

Button ($label = 'Submit', $style = 'primary')

A button used for actions and links.

Button::make('Register')->click('register'),
Button::make('Already registered?', 'secondary')->route('login'),
Button::make('Go back home', 'link')->url('/'),

The $style parameter accepts a bootstrap button style e.g. primary, outline-secondary, link, etc. Use the block method to make a button full width.

Available methods: block, click, href, route, url

Checkbox ($name, $label)

A checkbox field.

Checkbox::make('accept', 'I accept the terms'),
Checkbox::make('accept', 'I accept')->help('Please accept our terms'),
Checkbox::make('active', 'This user is active')->switch(),

Use the switch method to style the checkbox as a switch.

Available methods: switch, help, instant, defer, lazy, debounce, disabled

Checkboxes ($name, $label = null)

An array of checkbox fields.

Checkboxes::make('colors', 'Colors')->options(['Red', 'Green', 'Blue']),

Available methods: options, switch, help, instant, defer, lazy, debounce, disabled

Color ($name, $label = null)

A color picker field.

Color::make('hair_color', 'Hair Color'),

Available methods: small, large, help, instant, defer, lazy, debounce, disabled, readonly

Conditional

A statement used to conditionally show fields.

Conditional::if($this->data('color') == 'green', [
    Input::make('green', 'Green'),
])->elseif($this->data('color') == 'blue', [
    Input::make('blue', 'Blue'),
])->else([
    Input::make('red', 'Red'),
]),

Available methods: if, elseif, else

DynamicComponent ($name, $attrs = [])

A field used to display dynamic third-party components.

DynamicComponent::make('honey'),
DynamicComponent::make('honey', ['recaptcha' => true]),

This would translate to <x-honey/> and <x-honey recaptcha="recaptcha"/> in your form.

File ($name, $label = null)

A file upload field.

File::make('avatar', 'Avatar'),
File::make('photos', 'Photos')->multiple(),
File::make('documents', 'Documents')->multiple()->disk('s3'),

Use the multiple method to allow multiple file uploads. Optionally specify the filesystem disk to use via the disk method (used for linking to files, defaults to the filesystem config default).

Available methods: disk, multiple, help, disabled

Input ($name, $label = null)

An input field.

Input::make('name', 'Name'),
Input::make('phone')->placeholder('Phone')->type('tel'),
Input::make('email', 'Email')->type('email')->large(),
Input::make('price', 'Price')->type('number')->append('$')->prepend('.00'),

The type method accepts a standard HTML input type. As with other inputs, use small or large to resize an input. Input fields also support appends/prepends, and even plaintext.

Available methods: small, large, help, instant, defer, lazy, debounce, disabled, readonly, placeholder, type, append, prepend, plaintext

Radio ($name, $label = null)

A radio field.

Radio::make('gender', 'Gender')->options(['Male', 'Female']),

Available methods: options, switch, help, instant, defer, lazy, debounce, disabled

Select ($name, $label = null)

A select dropdown field.

Select::make('color', 'Color')->options(['Red', 'Green', 'Blue']),
Select::make('color', 'Color')->options([
    '#ff0000' => 'Red',
    '#00ff00' => 'Green',
    '#0000ff' => 'Blue',
])->instant(),
Select::make('user_id', 'User')->options(User::pluck('name', 'id')->toArray()),

Available methods: options, small, large, help, instant, defer, lazy, debounce, disabled, placeholder

Textarea ($name, $label = null)

A textarea field.

Input::make('bio', 'Biography'),
Input::make('bio', 'Biography')->rows(5),

Available methods: small, large, help, instant, defer, lazy, debounce, disabled, readonly, placeholder, rows

View ($name, $data = [])

Used to render a custom Blade view inside the form.

View::make('custom-view', ['hello' => 'world']),
You might also like...
Dynamic Laravel Livewire Bootstrap toasts.

Laravel Livewire Toasts This package allows you to dynamically show Bootstrap toasts via Laravel Livewire components. Documentation Requirements Insta

Laravel UI, Auth, & CRUD scaffolding package using Bootstrap & Livewire.
Laravel UI, Auth, & CRUD scaffolding package using Bootstrap & Livewire.

bastinald/ux Laravel UI, Auth, & CRUD scaffolding package using Bootstrap & Livewire. Features Automatic migrations Automatic routing Automatic passwo

A Laravel starter kit with auth scaffolding using Livewire & Bootstrap.

Zephyr This package is a Laravel starter kit with auth scaffolding using Livewire & Bootstrap. It was created for people who prefer using Livewire & B

Livewire component for dependant and/or searchable select inputs
Livewire component for dependant and/or searchable select inputs

Livewire Select Livewire component for dependant and/or searchable select inputs Preview Installation You can install the package via composer: compos

Laravel Livewire full page component routing.

Laravel Livewire Routes Laravel Livewire full page component routing. This package allows you to specify routes directly inside your full page Livewir

Livewire component that brings Spotlight/Alfred-like functionality to your Laravel application.
Livewire component that brings Spotlight/Alfred-like functionality to your Laravel application.

About LivewireUI Spotlight LivewireUI Spotlight is a Livewire component that provides Spotlight/Alfred-like functionality to your Laravel application.

Laravel Livewire component to show Events in a good looking monthly calendar
Laravel Livewire component to show Events in a good looking monthly calendar

Livewire Calendar This package allows you to build a Livewire monthly calendar grid to show events for each day. Events can be loaded from within the

Livewire component that brings Spotlight/Alfred-like functionality to your Laravel application.
Livewire component that brings Spotlight/Alfred-like functionality to your Laravel application.

About Wire Elements Spotlight Wire Elements Spotlight is a Livewire component that provides Spotlight/Alfred-like functionality to your Laravel applic

A dynamic table component for Laravel Livewire - For Slack access, visit:
A dynamic table component for Laravel Livewire - For Slack access, visit:

A dynamic Laravel Livewire component for data tables. Bootstrap 4 Demo | Bootstrap 5 Demo | Tailwind Demo | Demo Repository Installation You can insta

Comments
  • Update to readme

    Update to readme

    Not sure how to edit this myself, but the Textarea sample is displaying Input::make instead of Textarea::make

    Also, how do I go about making custom views? Or adding pagination to existing dataset for arrayables. Not sure how to include the $tags->links() without being able to edit the view.

    IE a list of tags that I want to add or remove to particular models.

    opened by jcc5018 1
  • SpatieTranslatable and Spatie MediaLibrary management

    SpatieTranslatable and Spatie MediaLibrary management

    @bastinald ,

    Many thanks for this great package.

    I am using SpatieTranslatable package and I am wondering how to implement using your package.

    I also use Spatie MediaLibrary and I am also wondering how to implement using your package.

    You mention in a post that you are working on a new version : is this will be compatible with this one and will we have to review all our forms ?

    Many thanks in advance for your feedback.

    opened by invaders-xx 0
Owner
null
A TALL-based Laravel Livewire component to replace the (multiple) select HTML input form with beautiful cards.

TALL multiselect cards A TALL-based Laravel Livewire component to replace the (multiple) select HTML input form with beautiful cards. Table of content

Frederic Habich 19 Dec 14, 2022
A dynamic Laravel Livewire component for multi steps form

Livewire component that provides you with a wizard that supports multiple steps form while maintaining state.

Vildan Bina 233 Jan 4, 2023
A demo of how to use filament/forms to build a user-facing Form Builder which stores fields in JSON.

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Dan Harrin 41 Dec 24, 2022
Composer package which adds support for HTML5 elements using Laravels Form interface (e.g. Form::date())

Laravel HTML 5 Inputs Composer package which adds support for HTML5 elements by extending Laravel's Form interface (e.g. Form::date()) Adds support fo

Small Dog Studios 11 Oct 13, 2020
Rapid form generation with Bootstrap 3 and Laravel.

Important: This package is not actively maintained. For bug fixes and new features, please fork. BootForms BootForms builds on top of my more general

Adam Wathan 423 Jun 14, 2022
A sample application with a multistep form built with Livewire.

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

David Grzyb 11 Nov 26, 2022
Laravel-comments-livewire - Livewire components for the laravel-comments package

Associate comments and reactions with Eloquent models This package contains Livewire components to be used with the spatie/laravel-comments package. S

Spatie 15 Jan 18, 2022
Sweetalert and Toaster notifications for Laravel livewire with support for tailwind and bootstrap.

Larabell Integrate livewire with sweetalert. Installation How to use Sweetalert Toast Available configuration Installation composer require simtabi/la

Simtabi 3 Jul 27, 2022
Free and open-source Laravel admin dashboard interface built with Livewire & Alpine.js based on Bootstrap 5

Volt Dashboard Laravel Free Frontend Web App for Laravel with Livewire & Alpine.js Never start a development project from scratch again. We've partner

Themesberg 200 Jan 4, 2023
Dynamic Laravel Livewire Bootstrap 5 modals.

Laravel Livewire Modals Dynamic Laravel Livewire Bootstrap 5 modals. Requirements Bootstrap 5 Installation Require the package: composer require basti

null 55 Dec 27, 2022