Laravel 9 Form Validation Example

Websolutionstuff | Feb-12-2022 | Categories : Laravel

 In this tutorial, we will see laravel 9 form validation example. For any incoming data, we need to validate it before storing it in the database. Laravel provides several different approaches to validate your application's incoming data. It is most common to use the validate method available on all incoming HTTP requests.

Laravel 9 includes a wide variety of convenient validation rules that you may apply to data for validation. In laravel 9 you can also add error message in blade files, custom validation in laravel 9.

So, let's see form validation in laravel 9, laravel 9 custom validation rule, laravel 9 validation error messages, custom validation message in laravel 9.

Here, we will use has() function in the session to check the error message in laravel 9. Using this example you can check simple form validation as well as you can create your own custom validation in laravel 9.

Step 1 : Add Route

First, we have the following routes defined in our routes/web.php file.


use App\Http\Controllers\UserController;
Route::get('/user/create', [UserController::class, 'create']);
Route::post('/user/store', [UserController::class, 'store']);



Step 2 : Create Controller

Now, we will create UserController to save data in the database. So, copy the below code in your controller.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class UserController extends Controller
    public function create()
    	return view('index');

    public function store(Request $request)
        $validatedData = $request->validate([
                'name' => 'required',
                'password' => 'required|min:8',
                'email' => 'required|email|unique:users'
            ], [
                'name.required' => 'Name is required',
                'password.required' => 'Password is required',
                'email.required' => 'Email is required'
        $validatedData['password'] = bcrypt($validatedData['password']);
        $user = User::create($validatedData);
        return back()->with('success', 'User Created Successfully !!');

In laravel 9 Alternatively, validation rules may be specified as arrays of rules instead of a single | delimited string.

$validatedData = $request->validate([
    'name' => 'required',
    'password' => ['required', 'min:8'],
    'email' => ['required, email, unique:users'],



Stopping On First Validation Failure

Sometimes we need to stop running validation rules on an attribute after the first validation failure. To do so, assign the bail rule to the attribute in laravel 9.

    'name' => 'bail|required|unique:users|max:255',
    'password' => ['required', 'min:8'],
    'email' => ['bail, required, email, unique:users'],

In this example, if the unique rule on the name attribute fails, the max rule will not be checked. Rules will be validated in the order they are assigned. 


Step 3 : Add Blade File

In this step, we will create a blade file and save the name as index.blade.php

    <title>Laravel 9 Form Validation Example - Websolutionstuff</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="" rel="stylesheet">
    <div class="container">  
        <div class="row">
            <div class="col-md-12" style="margin-top: 30px;">
                <h1>Laravel 9 Form Validation Example - Websolutionstuff</h1>   
                <div class="alert alert-success">
                    {{ Session::get('success') }}
                <form method="POST" action="{{ url('user/store') }}">
                    <div class="form-group">
                        <input type="text" name="name" class="form-control" placeholder="Name">
                        @if ($errors->has('name'))
                            <span class="text-danger">{{ $errors->first('name') }}</span>
                    <div class="form-group">
                        <input type="password" name="password" class="form-control" placeholder="Password">
                        @if ($errors->has('password'))
                            <span class="text-danger">{{ $errors->first('password') }}</span>
                    <div class="form-group">
                        <input type="text" name="email" class="form-control" placeholder="Email">
                        @if ($errors->has('email'))
                            <span class="text-danger">{{ $errors->first('email') }}</span>
                    <div class="form-group">
                        <button class="btn btn-info btn-submit">Submit</button>



Output :



You might also like :

Recommended Post
Featured Post
How To Image Upload With Preview In Angular 15
How To Image Upload With Previ...

In today's world of web development, being able to upload images with a preview feature is a common requirement for...

Read More


How to Install Select2 in Laravel 10 Example
How to Install Select2 in Lara...

Hey there, Laravel enthusiasts! If you're looking to add a touch of elegance and user-friendliness to your web...

Read More


Laravel 8 CRUD Operation Example
Laravel 8 CRUD Operation Examp...

In this article, we will see the laravel 8 crud operation. As you know Laravel 8 has already been officially released an...

Read More


Send Email In Laravel
Send Email In Laravel

In this article, we will explore the process of sending emails in Laravel, covering versions 6, 7, 8, 9, and 10. Email f...

Read More