In this tutorial you will learn about the Laravel 8 Custom Validation Error Messages Tutorial and its application with practical example.
In this Laravel 8 Custom Validation Error Messages Tutorial I will show you how to create custom validation message error in laravel projects. In this tutorial you will learn to customize default validation message and use custom validation error messages in laravel forms. In this article I will share example on how to create and use validation error messages on laravel.
Custom Validation Error Messages In Laravel
In this step by step guide I will demonstrate you how to use custom validation error messages on laravel forms. Please follow the instruction given below:
- Step 1 – Install Laravel 8 App
- Step 2 – Connecting App to Database
- Step 3 – Run Migration Command
- Step 4 – Add Routes
- Step 5 – Generate Controller By Command
- Step 6 – Create the blade view
- Step 7 – Run Development Server.
Step 1 – Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel Blog |
Step 2 – Connecting App to Database,
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
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
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
Step 4 – Add Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 |
use App\Http\Controllers\CustomErrorController; Route::get('form', [CustomErrorController::class, 'index']); Route::get('store', [CustomErrorController::class, 'store']); |
Step 5 – Generate Controller By Command.
Now, lets create a controller named CustomErrorController using command given below –
1 |
php artisan make:controller CustomErrorController |
Once the above command executed, it will create a controller file CustomErrorController.php in app/Http/Controllers/ directory. Open the CustomErrorController.php file and put the following code in it.
app/Http/Controllers/CustomErrorController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
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 we will create blade view file. Go to resources/views folder and create a blade view file from.blade.php and update the following code into your file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<!DOCTYPE html> <html> <head> <title>Laravel 8 Custom Validation Error Message Example 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 rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <div class="container"> <h1>Laravel 8 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.
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/form |