Define Validation in Laravel with Example


In this tutorial i’m going to describe what is Validation and how to using in to project.

Laravel provides several different approaches to validate your application’s incoming data. By default, Laravel’s base controller class uses a ValidatesRequests trait which provides a convenient method to validate incoming HTTP request with a variety of powerful validation rules.

Laravel provide several pre-define validation rules that way we can user it simply. In this example i used some validation rules that listed bellow that need to apply.

1)required: for must be required field.

2)min: for limit to enter minimum character.

3)max: for limit to enter maximum character.

4)mail: for check only email allow.

5)unique: for check with database table unique column.

6)numeric: for allow only numeric value.

7)same: for two fields value must be match.

Let’s first to install Project

composer create-project laravel/laravel learning-project "5.8.*"

After installation migrate the tables

php artisan migrate

Next go to your routes/web.php and paste below code

Route::get('form-validation', 'HomeController@formValidation');
Route::post('form-validation', 'HomeController@formValidationPost');

Next to Add Controller

Create HomeController

php artisan make:controller HomeController

Ok, now we write two method in HomeController as listed bellow:



In first method formValidation() we will just return view and in second method formValidationPost() we will write from validation rules.

I use $this->validate() method of Controller class and it take three argument like as bellow syntax:

$this->validate(Request Object, Validation Rules Array, Validation Rules Custom Message);

Next go to your Controller file and paste below code



namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
public function formValidation(){

return view('form-validation');

public function formValidationPost(Request $request)
'firstname'=> 'required|min:5|max:35',
'lastname' => 'required|min:5|max:35',
'email'=> 'required|email|unique:users',
'mobileno'=> 'required|numeric',
'password'=> 'required|min:3|max:20',
'confirm_password'=> 'required|min:3|max:20|same:password',
'details'=> 'required'

'firstname.required' => ' The first name field is required.',
'firstname.min' => ' The first name must be at least 5 characters.',
'firstname.max' => ' The first name may not be greater than 35 characters.',
'lastname.required' => ' The last name field is required.',
'lastname.min' => ' The last name must be at least 5 characters.',
'lastname.max' => ' The last name may not be greater than 35 characters.',

dd('you are added all the fields');


Next to create blade files resources/views/form-validation.blade.php

<!DOCTYPE html>
<title>Form Validation in Laravel by Laravel Amit</title>
<link rel="stylesheet" type="text/css" href="">
<div class="container">
<h2>Form Validation</h2>
<form method="POST" action="/form-validation" autocomplete="off">

<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.
@foreach($errors->all() as $error)
<li>{{ $error }}</li>

<input type="hidden" name="_token" value="{{ csrf_token() }}">

<div class="row">
<div class="col-md-6">
<div class="form-group {{ $errors->has('firstname') ? 'has-error' : '' }}">
<label for="firstname">First Name:</label>
<input type="text" id="firstname" name="firstname" class="form-control" placeholder="Enter First Name" value="{{ old('firstname') }}">
<span class="text-danger">{{ $errors->first('firstname') }}</span>
<div class="col-md-6">
<div class="form-group {{ $errors->has('lastname') ? 'has-error' : '' }}">
<label for="lastname">Last Name:</label>
<input type="text" id="lastname" name="lastname" class="form-control" placeholder="Enter Last Name" value="{{ old('lastname') }}">
<span class="text-danger">{{ $errors->first('lastname') }}</span>

<div class="row">
<div class="col-md-6">
<div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
<label for="email">Email:</label>
<input type="text" id="email" name="email" class="form-control" placeholder="Enter Email" value="{{ old('email') }}">
<span class="text-danger">{{ $errors->first('email') }}</span>
<div class="col-md-6">
<div class="form-group {{ $errors->has('mobileno') ? 'has-error' : '' }}">
<label for="mobileno">Mobile No:</label>
<input type="text" id="mobileno" name="mobileno" class="form-control" placeholder="Enter Mobile No" value="{{ old('mobileno') }}">
<span class="text-danger">{{ $errors->first('mobileno') }}</span>
<div class="row">
<div class="col-md-6">
<div class="form-group {{ $errors->has('password') ? 'has-error' : '' }}">
<label for="password">Password:</label>
<input type="password" id="password" name="password" class="form-control" placeholder="Enter Password" >
<span class="text-danger">{{ $errors->first('password') }}</span>
<div class="col-md-6">
<div class="form-group {{ $errors->has('confirm_password') ? 'has-error' : '' }}">
<label for="confirm_password">Confirm Password:</label>
<input type="password" id="confirm_password" name="confirm_password" class="form-control" placeholder="Enter Confirm Passowrd">
<span class="text-danger">{{ $errors->first('confirm_password') }}</span>

<div class="row">
<div class="col-md-12">
<div class="form-group {{ $errors->has('details') ? 'has-error' : '' }}">
<label for="details">Details:</label>
<textarea name="details" id="details" class="form-control" placeholder="Enter Details">{{ old('details') }}</textarea>
<span class="text-danger">{{ $errors->first('details') }}</span>

<div class="form-group">
<button class="btn btn-success">Submit</button>

Now Blade files as look like

Now fill your input


Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x