Laravel 11 Livewire 3 Form Validation Example

Websolutionstuff | Jan-30-2025 | Categories : Laravel

Form validation is a crucial part of building secure and user-friendly web applications. With Laravel 11 and Livewire 3, you can implement dynamic, real-time form validation effortlessly.

Whether you need to validate inputs on the fly or customize validation rules, Livewire makes it simple and powerful.

In this guide, I’ll show you how to set up form validation in Livewire 3 step by step.

Form Validation Laravel Livewire 3

Form Validation Laravel Livewire 3

 

Step 1: Create ProductCreate Component

In this step, we'll create a Livewire component using their command.

php artisan make:livewire ProductCreate
app/Livewire/ProductCreate.php
   
resources/views/livewire/product-create.blade.php

Now, both files we will update.

app/Livewire/ProductCreate.php

<?php

namespace App\Livewire;

use Livewire\Component;
use App\Models\Product;

class ProductCreate extends Component
{
    public $name, $price, $detail;

    public function render()
    {
        return view('livewire.product-create');
    }

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function submit()
    {
        $this->validate([
            "name" => "required",
            "price" => "requierd|min:5|numeric",
            "detail" => "required"
        ]);

        $product = Product::create([
            "name" => $this->name,
            "price" => $this->price,
            "detail" => $this->detail
        ]);

        info($product);
    }
}

resources/views/livewire/product-create.blade.php

<div>

    <form wire:submit="submit">
        
        <label>Name:</label>
        <input type="text" name="name" class="form-control" wire:model.change="name">
        @error("name")
        <p class="text-danger">{{ $message }}</p>
        @enderror

        <label>Price:</label>
        <input type="text" name="price" class="form-control" wire:model="price">
        @error("price")
        <p class="text-danger">{{ $message }}</p>
        @enderror

        <label class="mt-1">Detail:</label>
        <textarea class="form-control" wire:model="detail"></textarea>
        @error("detail")
        <p class="text-danger">{{ $message }}</p>
        @enderror

        <button class="btn btn-success mt-3" wire:loading.attr="disabled" type="submit">Submit</button>

    </form>
</div>

 
Step 2: Use Livewire Component

Now, we'll use the Product component on the home page.

resources/views/home.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Product Create') }}</div>

                <div class="card-body">
                    <livewire:product-create />
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

 


You might also like:

Recommended Post
Featured Post
Laravel 9 Vue JS Search Example
Laravel 9 Vue JS Search Exampl...

In this article, we will see a laravel 9 vue js search example. Here, we will learn how to live search in laravel 9 usin...

Read More

Dec-14-2022

File Upload With Progress Bar In Angular 13
File Upload With Progress Bar...

In this article, we will see the file upload with a progress bar in angular 13. In this example, we will learn how to im...

Read More

Jun-11-2022

How to Install and Configure Debugbar in Laravel 11
How to Install and Configure D...

Hello, laravel web developers! In this article, we'll see how to install and configure the debugbar in laravel...

Read More

Aug-02-2024

How To Import Export Excel & CSV File In Laravel 10
How To Import Export Excel & C...

In this article, we will see how to import and export Excel & CSV files in laravel 10. Here, we will learn about lar...

Read More

Mar-08-2023