Laravel Validation Custom Error Messages Tutorial




Laravel Validation Custom Error Messages Tutorial

Hello Friends,

In this example, you will learn laravel validation custom error messages tutorial. We will look at an example of custom error message laravel validator. if you have a question about laravel custom validation message in a controller then I will give a simple example with solution. you will learn laravel form validation custom error messages. you will do the following things for laravel change validation error message.

The function in it is getting 3 values: attribute, value, and fail. The attribute is the field for which the validation is happening. The value corresponds to the actual value of the said object and failure is the callback method that would be executed once the validation fails.

You can use this example with laravel 6, laravel 7, laravel 8, and laravel 9 versions.

You have just to follow the below step and you will get the layout as below:

Step 1: Install Laravel

This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Database Configuration

In this step we have to make database configuration for the example database name, email, password, etc. So let's open the .env file and fill in all details like as below:





DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

Step 3: Create CustomError Controller

Run below command to create CustomError controller

php artisan make:controller CustomErrorController



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class CustomErrorController extends Controller


public function index()


return view('form');


public function store(Request $request)




'name' => 'required',

'password' => 'required',

'email' => 'required'



'name.required' => 'Name is required',

'password.required' => 'Password is required'



$input = $request->all();

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

$user = User::create($input);

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



Step 4: Create CustomError Route



use Illuminate\Support\Facades\Route;

use App\Http\Controllers\CustomErrorController;



| Web Routes



| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!



Route::get('form', [CustomErrorController::class, 'index'])->name('form');

Route::post('store', [CustomErrorController::class, 'store'])->name('store');

Run the migration command to migrate User Table:

php artisan migrate

Step 5: Create Carousel Slider View

let's create form.blade.php(resources/views/form.blade.php) for form and we will write design code here and put the following code:


<!DOCTYPE html>



<title>Laravel Validation Custom Error Messages Tutorial -</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" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">



background-color: #d2d2d2;



background-color: #fff;





<div class="container">

<div class="row">

<div class="col-md-12 mt-5">

<div class="row">

<form method="POST" action="{{ route('store') }}" class="border rounded p-5 mt-5" autocomplete='off'>


<div class="col-md-12">

<div class="row">

<h4 class="col-md-7 ps-2 m-0">Laravel Validation Custom Error Messages Tutorial</h4>

<strong class="col-md-5 p-0 m-0 text-end" style="color: #008B8B"></strong>


<div class="alert alert-success">

{{ Session::get('success') }}








<div class="col-md-12 mt-2">

<label for="name" class="form-label mb-0">Name:</label>

<input type="text" name="name" class="form-control" placeholder="Name">

@if ($errors->has('name'))

<span class="text-danger">{{ $errors->first('name') }}</span>



<div class="col-md-12 mt-2">

<label for="email" class="form-label mb-0">Email:</label>

<input type="text" name="email" class="form-control" placeholder="Email">

@if ($errors->has('email'))

<span class="text-danger">{{ $errors->first('email') }}</span>



<div class="col-md-12 mt-2">

<label for="password" class="form-label mb-0">Password:</label>

<input type="password" name="password" class="form-control" placeholder="Password">

@if ($errors->has('password'))

<span class="text-danger">{{ $errors->first('password') }}</span>



<div class="col-md-12">

<button class="col-md-12 btn btn-success btn-submit mt-3">Submit</button>









Step 6: Start Development Server

Start the development server. Use the PHP artisan serve command and start your server:

php artisan serve

Now you are ready to run our example so run the below command to quick run.



I hope it can help you...