The easiest way to implement custom validation rules and error messages in Laravel 11 /10 is just to open your controller, define custom validation rules, and open your blade view to define custom error messages in laravel applications.
How to Create Custom Validation Rules and Error Messages Laravel 11 / 10
Steps to add custom validation rules with messages and display custom error messages in Laravel 11 / 10 blade views:
Step 1 – Create New Laravel 10 Project
First of all, Open your terminal or the command prompt.
Then execute the following command into it to download or install Laravel 10 new setup:
composer create-project --prefer-dist laravel/laravel Blog
Step 2 – Setup Database with Laravel Project
Once you have installed and set up laravel project on server. Then you need to configure your database with your apps.
So, visit your app root directory and find .env file. Then configure database details as follows:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here
Step 3 – Run Migration Command
In this step, execute the following command on terminal or cmd to create tables into database:
php artisan migrate
This command will create some tables into your database.
Step 4 – Define Routes
In this step, Open your routes/web.php and update the following routes into your routes/web.php file:
routes/web.php
use App\Http\Controllers\CustomErrorController; Route::get('form', [CustomErrorController::class, 'index']); Route::get('store', [CustomErrorController::class, 'store']);
Step 5 – Generate Controller By Command
In this step, execute the following command on terminal or command prompt to create a custom error message controller in laravel app:
php artisan make:controller CustomErrorController
Once you have executed the above given command. Then you need to go app/Http/Controllers directory and open CustomErrorController.php file and update the following code into your controller file:
app/Http/Controllers/CustomErrorController.php
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class CustomErrorController extends Controller { public function create() { return view('form'); } public function store(Request $request) { $request->validate( [ 'name' => 'required', 'password' => 'required|min:5', 'email' => 'required|email|unique:users' ], [ '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 6 – Create Blade View
In this step, Go to resources/views folder and create one blade view file name from.blade.php and update the following code into your file:
<!DOCTYPE html> <html> <head> <title>Laravel 10 Custom Validation Error Message Example Tutorial -Tutsmake.com</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 rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <div class="container"> <h1>Laravel 10 Custom Validation Error Message Example</h1> @if(Session::has('success')) <div class="alert alert-success"> {{ Session::get('success') }} @php Session::forget('success'); @endphp </div> @endif <form method="POST" action="{{ route('store') }}"> @csrf <div class="form-group"> <label>Name:</label> <input type="text" name="name" class="form-control" placeholder="Name"> @if ($errors->has('name')) <span class="text-danger">{{ $errors->first('name') }}</span> @endif </div> <div class="form-group"> <label>Password:</label> <input type="password" name="password" class="form-control" placeholder="Password"> @if ($errors->has('password')) <span class="text-danger">{{ $errors->first('password') }}</span> @endif </div> <div class="form-group"> <strong>Email:</strong> <input type="text" name="email" class="form-control" placeholder="Email"> @if ($errors->has('email')) <span class="text-danger">{{ $errors->first('email') }}</span> @endif </div> <div class="form-group"> <button class="btn btn-success btn-submit">Submit</button> </div> </form> </div> </body> </html>
Step 7: Run Development Server
In this step, Execute the PHP artisan serve command on the terminal or cmd to start server locally:
php artisan serve
If you want to run the project diffrent port so use this below command
php artisan serve --port=8080
Then open your browser and hit the following url on it:
http://localhost:8000/form