How to Make Form Input Validation rules in Laravel 8 ?

Today now in this post i will show you How to Make Form Input Validation rules in Laravel 8 ? Here I am going to teach you how we can make input form validation rules on Laravel application. We always need to use the form also with validation in our Laravel application. So now we will learn about how we can implement validation rules and also how we can print all error message on blade files.

Laravel is provide us several pre-define validation rules on that way we can easily validated. Now In this example i will used some validation rules on that listed bellow that need to apply on our application.

  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.

So, now in this example i will give you one small example from the scratch. So by using this we can simply understand how to set validation rules in Laravel blade template even if any one is beginner.

So, let’s start and follow few step:

Step 1: Add Route

Now in first step we need to add two new routes in our fresh Laravel application. So open your routes file and then add the bellow two routes.

routes/web.php

Route::get('form-validation', 'UserInfoController @formValidation');
Route::post('form-validation', 'UserInfoController @formValidationPost');
Step 2: Add Controller

Now in this step we need to create a new UserInfoController and then we will write two new method with validation rules. So at first need to create new UserInfoController by using the bellow command:

php artisan make:controller UserInfoController 

Ok, now we need to write two method in UserInfoController as listed bellow:

1)formValidation()

2)formValidationPost()

Now in first method formValidation() we will just return a view file and in the second method formValidationPost() we will write all the from validation rules.

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

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

1)Request Object: We need to just pass the whole object of Request.

2)Validation Rules Array: On second argument we need to pass array of the validation rules.

3)Validation Rules Custom Message: In the third argument we need to pass an array of custom message.

Ok, so we need to just copy the bellow code and put it into our UserInfoController file.

app/Http/Controllers/UserInfoController.php

<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;
use App\Http\Requests;


class UserInfoController  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 successfully added all fields.');
    }
}
Step 3: Add Blade File

Now in the last step we need to create a new form-validation.blade.php file and i will write code of bootstrap layout. Also in this file i will write code for how to print the error message code and also know how to get the old value from input. Here I will explain it as bellow:

1)$errors: Here we can get the whole validation error from the $errors variable. We can see in the bellow how can i will display the whole message:

@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

2)has(): Here I will use has() method of $errors object, on that way i can also check the message is exist or not. It take only single argument. If “firstname” field validation the true then it will add on “has-error” class will add, otherwise it will be an empty string. In bellow you can see how it use:

{{ $errors->has('firstname') ? 'has-error' : '' }}

3)first(): I can also use first() method of $errors object, on that way you can get a single message field wise. It is take only one argument. you can see this in the bellow:

{{ $errors->first('firstname') }}

4)old(): We can also use old() helper function of Laravel, Using this function will return the old value of that field, when the validation will be false then we don’t need to write the other field again and again.

{{ old('firstname') }}

resources/views/form-validation.blade.php

<!DOCTYPE html>
<html>
<head>
	<title>Laravel - Form Validation</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>

Read Also: How to implement infinite ajax scroll pagination in Laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook

Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.