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:

1)formValidation()

2)formValidationPost()

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

app/Http/Controllers/HomeController.php

<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;class HomeController extends Controller{    public function formValidation(){        return view('form-validation');    }    public function formValidationPost(Request $request)    {        $this->validate($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><html><head>	<title>Form Validation in Laravel by Laravel Amit</title>	<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></head><body><div class="container">	<h2>Form Validation</h2>	<form method="POST" action="/form-validation" autocomplete="off">		@if(count($errors))			<div class="alert alert-danger">				<strong>Whoops!</strong> There were some problems with your input.				<br/>				<ul>					@foreach($errors->all() as $error)					<li>{{ $error }}</li>					@endforeach				</ul>			</div>		@endif		<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>			</div>			<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>			</div>		</div>		<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>			</div>			<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>			</div>		</div>		<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>			</div>			<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>			</div>		</div>		<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>			</div>		</div>		<div class="form-group">			<button class="btn btn-success">Submit</button>		</div>	</form></div></body></html>

Now Blade files as look like

Now fill your input

Thanks.

Hi I am Amit Experienced Web Developer with a demonstrated history of working in the information technology and services industry.

Related Posts

Blade in Laravel

Blade is the simple,yet powerful templating engine that is included with laravel. Blade does not restrict you from using plain php code in your templates. Blade templates…

Read More

CRUD USING QUIRY BUILDER IN LARAVEL.

We are performing crud opertion using quiry builder in laravel STEP : 1 Create Laravel project by running this command in terminal CMND :– Laravel new project_name…

Read More

Controller In Laravel

Controllers can group related request handling logic into a single class, Instead of defining all of your request handling logic as closure in route files ,you may…

Read More

LARAVEL ROUTING

All Laravel routes are define in route files , which are located in the route directory ,These file are automatically loaded by the framework. The most basic…

Read More

MVC (MODEL VIEW CONTROLLER)

MVC stands for Model–view–controller. The MVC is an architectural pattern model that seprates an applications into three logical components.(Model , View, Controller) MVC was first introduce in…

Read More

ReflectionException : Class BlogsTableSeeder does not exist

When i type php artisan db:seed command. It’s showing this type of errors. [ReflectionException]Class UserTableSeeder does not exist Here is my BlogsTableSeeder <?phpuse Illuminate\Database\Seeder;class BlogsTableSeeder extends Seeder{…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x