In this laravel 7 custom validation error messages example tutorial, you will learn how to add custom validation message error in laravel web applications.
Sometimes, you need to change the default validation message and want to use custom validation error messages with laravel forms instead of dafault validation error message in laravel.
So, this tutorial will help you step by step on how to use custom validation error messages on laravel forms.
Laravel Custom Validation Error Message Example
Just follow the below steps and learn how to use laravel custom validation error messages on laravel forms:
- Step 1: Install Laravel Application
- Step 2: Setup Database Credentials
- Step 3: Run Migration Command
- Step 4: Create Route
- Step 5: Generate Controller By Command
- Step 6: Create the blade view
- Step 7: Run Development Server
Step 1: Install Laravel Application
First of all, Open your command prompt and run the following command:
composer create-project --prefer-dist laravel/laravel Blog
This command will install fresh new laravel setup in provided location. After that, open your laravel web application in any text editor and Go to your application .env file and set up database credentials and move next step.
Step 2: Setup Database Credentials
In this step, Add database credentials in the .env file:
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
Next, Open your command prompt and run the following command for migration:
php artisan migrate
This command will create some tables into your database.
Step 4: Create Routes
In this step, Open your routes/web.php and update the following routes into your routes/web.php file:
routes/web.php
Route::get('form', 'CustomErrorController@create');
Route::post('store', 'CustomErrorController@store');
Step 5: Generate Controller By Command
In this step, open your command prompt and run the following command to generate custom error message controller:
php artisan make:controller CustomErrorController
After that, Go to app/Http/Controllers/CustomErrorController.php and update the following code into your controller file:
app/Http/Controllers/CustomErrorController.php
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:
resources/views/form.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 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 href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Laravel 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, we will use the PHP artisan serve command. It will start your server locally
php artisan serve If you want to run the project diffrent port so use this below command php artisan serve --port=8080
Now we are ready to run our example so run bellow command to quick run.
http://localhost:8000/form