Laravel 10 form validation with custom message and display errors
Laravel provides us some nice way to validate form input fields and display error message. In this blog we will validate our form fields and set custom message for each field. If you want to set custom message to your form validation then you can continue this post because i will show you how to set and display custom message for laravel 10 validation.
After make validation we will set custom message for each validation rules and also display the validation error messages above the form. For doing this bellow steps are required.
1. Create laravel project.
2. Create a form
3. Create routes
4. Create a controller and make validation
5. Test validation
Step 1. Create laravel project.
Initialy we need to create a project using laravel installer or we can build it by composer. It's your choice how you create a project. Let us make a project form-validation inside xampp\htdocs.
//BY laravel installer
laravel new form-validation
//By composer
composer create-project laravel/laravel form-validation
Step 2. Create a form
Once the form-validation project installation is successful we will create a new blade file inside resources\views or you can use the default page i.e welcome.blade.php file for design your form. I'm creating a form in the welcome.blade.php file. So open your blade file in editor and paste the entire bellow code.
Note: I included bootstrap cdn link for a better design
resources\views\welcome.blade.php
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Laravel 9 form validation with custom message</title>
</head>
<body>
<div class="container">
<div class="row mt-3">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<div class="title">
<h3 class="text-left">Laravel 9 form validation with custom message</h3>
</div>
</div>
<div class="card-body">
{{-- display validation error message --}}
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('validate.form') }}" method="post" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-md-5">
<div class="mb-3">
<label class="mb-1">Your Name</label>
<input type="text" name="name" class="form-control" value="">
</div>
<div class="mb-3">
<label class="mb-1">Your Email</label>
<input type="email" name="email" class="form-control" value="">
</div>
<div class="col-md-5">
<div class="form-check-inline mb-3">
<label class="form-check-label mb-1">Your Gender</label> <br>
<input class="form-check-input" type="radio" name="gender" value="Male"> Male
<input class="form-check-input" type="radio" name="gender" value="Female"> Female
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Step 3. Create routes
Once the form design completed we have to create routes for validation. let's change our routes\web.php file is as bellow.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ValidationController;
Route::get('/',[ValidationController::class,'welcome'])->name('welcome');
Route::post('/validate-form',[ValidationController::class,'validate_form'])->name('validate.form');
Routes create is complete, the first route is for return welcome.blade.php file and second route is for submitting form.
Step 4. Create a controller and make validation
Now we need to creatae a controller ValidationController with the artisan command and after that we will apply all validation rules inside controller.
php artisan make:controller ValidationController
Ok, let's return welcome page first from controller.
app\Http\Controllers\ValidationController
public function welcome()
{
return view('welcome');
}
It's time to validate our form using laravel validation rules in controller.
app\Http\Controllers\ValidationController
public function validate_form(Request $request)
{
$request->validate([
'name'=>'required|regex:/^[\pL\s\-]+$/u|max:50',
'email'=>'required|regex:/(.+)@(.+)\.(.+)/i|email|max:50',
'gender'=>'required'
],[
'name.required' => __('Name is required'),
'name.regex' => __('Only alphabet is acceptable'),
'email.required' => __('Email is required'),
'gender.required' => __('Gender is required'),
]);
}
Complete code in ValidationController.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ValidationController extends Controller
{
public function welcome()
{
return view('welcome');
}
public function validate_form(Request $request)
{
$request->validate([
'name'=>'required|regex:/^[\pL\s\-]+$/u|max:50',
'email'=>'required|regex:/(.+)@(.+)\.(.+)/i|email|max:50',
'gender'=>'required'
],[
'name.required' => __('Name is required'),
'name.regex' => __('Only alphabet is acceptable'),
'email.required' => __('Email is required'),
'gender.required' => __('Gender is required'),
]);
}
}
Step 5. Test validation
Note: Before submit form don't forget run artisan server
Php artisan serve
Click on Submit button to test your app and you will see validation error message display with custom messages.