In this Laravel 9 validation tutorial, I'll demonstrate how to validate form input and provide an error message before saving it to the database. Therefore, Laravel 9 will sanities our form data before saving it to the database. This example is for you if you don't know how to validate form input in Laravel 9.

You will therefore learn how to implement the Laravel 9 form validation lesson from this Laravel 9 validation tutorial. Laravel 9 form validation will appear, along with an error notice. To create custom error messages for Laravel 9 form validation, simply follow the steps below.

You are aware that Laravel 9 offers a request object so you can use it to provide form validation. I'll use this request validate() to add custom messages and validation rules. Check out the example for adding form validation below.

Download?Laravel 9

To implement this form validation in Laravel 9, you must first download a Laravel 9 application. So use the command below to download it.

composer create-project laravel/laravel example-app

Create Route

Currently, add the routes to control GET and POST requests for call view, as well as add form validation, to the routes/web.php file.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

Route::controller(UserController::class)->group(function () {
    Route::get('/', 'create')->name('user.create');
    Route::post('/', 'store')->name('');

Create Controller

In order to provide form validation, we will develop a new UserController in this step. We will include two methods called create() and store in this controller (). So let's run the following command to build a new controller.


namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Blade;

class UserController extends Controller
    public function create()
        return Blade::render('welcome');

    public function store(Request $request)
        $data = $request->validate([
            'name' => 'required',
            'password' => 'required|min:5',
            'email' => 'required|email|unique:users'
        ], [
            'name.required' => 'Name field is required.',
            'password.required' => 'Password field is required.',
            'email.required' => 'Email field is required.',
            '' => 'Email field must be email address.'

        $data['password'] = bcrypt($data['password']);

        return back()->with('success', 'User created successfully.');


Create View

We now need to develop a view form in this last stage in order to validate our form input. I'll build a straightforward bootstrap form with an error message. Let's make the following file, then:


<div class="container">
    <h1>Laravel 9 Form Validation Example -</h1>
    <div class="alert alert-success">
        {{ Session::get('success') }}

    Way 1: Display All Error Messages
    @if ($errors->any())
        <div class="alert alert-danger">
            <strong>Whoops!</strong> There were some problems with your input.<br><br>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
    <form method="POST" action="{{ route('') }}">
        <div class="mb-3">
            <label class="form-label" for="inputName">Name:</label>
                class="form-control @error('name') is-invalid @enderror" 

            Way 2: Display Error Message
                <span class="text-danger">{{ $message }}</span>
        <div class="mb-3">
            <label class="form-label" for="inputPassword">Password:</label>
                class="form-control @error('password') is-invalid @enderror" 

            Way 3: Display Error Message
            @if ($errors->has('password'))
                <span class="text-danger">{{ $errors->first('password') }}</span>
        <div class="mb-3">
            <label class="form-label" for="inputEmail">Email:</label>
                class="form-control @error('email') is-invalid @enderror" 

                <span class="text-danger">{{ $message }}</span>
        <div class="mb-3">
            <button class="btn btn-success btn-submit">Submit</button>

I hope this lesson on Laravel 9 form validation with error message was helpful.


Recommended Posts

View All

Laravel 9 generate RSS Feed Tutorial With Example

Learn how to generate RSS feeds in Laravel 9 with our easy-to-follow tutorial. Get step-by-step instructions and real-world examples. Start today!

Laravel One to Many Eloquent Relationship

laravel one to many relationship example, laravel one to many sync, one to many relationship laravel 5.6, one to many relationship laravel model

Laravel Eloquent WHERE Like Query Example Tutorial

Laravel provides a query builder that helps us to deal with such a situation in MySQL. In this tutorial, you'll learn how to use a select where like q...

Laravel 8 Custom 404, 500 Error Page Example

How to create custom error page in Laravel 8 and we will also try to tell you why we required to create the custom error page.

How to Get Current URL in Laravel

how to get current url in laravel, laravel get current url, get current page url in laravel, get full url in laravel, find current url in laravel,