Laravel 9 Form Validation Example With Error Message

Hello Artisan,

In this Laravel 9 validation tutorial, I am going to show you how to validate form data before saving it to the database with an error message. So we will sanitize our form data before saving it into the database in Laravel 9. So if you don't know how to validate form data in Laravel 9 then this example is for you.

So from this Laravel 9 validation tutorial, you will learn how to implement laravel 9 form validation tutorial. We will see laravel 9 form validation with an error message. Just you have to follow the below step for laravel 9 form validation custom error messages.

You know that Laravel 9 provides a request object to add form validation using it. I will use this request validate() for adding validation rules and custom messages. Let's see the below example for adding form validation.




Step 1: Download Laravel 9

First of all, you need to download a Laravel 9 application to create this form validation in Laravel 9. So download it via the below command.

composer create-project laravel/laravel example-app


Step 2: Create Route

Now, open the routes/web.php file and add the routes to manage GET and POST requests for call view and add form validation.


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('');


Step 3: Create Controller

In this step, we will create a new UserController to add form validation. in this controller, we will add two methods called create() and store(). so let's create a new controller by the following command.


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.');



Step 4: Create View

Now in this final step, we have to create a view form to validate our form data. I will create a bootstrap simple form with an error validation message. So, let's create the following file:



Read also: Create Your First Laravel 9 CRUD Application Step By Step


Hope this Laravel 9 form validation with error message tutorial will help you.


Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js