Laravel 5.3 - Form Input Validation rules example with demo

By Hardik Savani November 5, 2023 Category : Laravel

Today, I am going to give you how to use input form validation rules of Laravel 5.3. We always require to use form with validation in our Laravel application. So today you can learn how to implement validation rules and how to print error message on blade files.

Laravel 5 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.

In bellow seven rules i used in my example but there are several other pre-define rules in Laravel 5 and we can use it.

So, in this example i give you one small example from scratch so you can simply understand how to set validation rules in Laravel blade template even if you are beginner. So, let's follow few step:

Preview:

Step 1: Add Route

In first step we have to add two routes in our fresh Laravel 5.3 application. So open your routes file and add bellow two routes.

routes/web.php

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

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

Step 2: Add Controller

Ok, In this step we have to create new HomeController and we will write two method with validation rules. So first create new HomeController by using bellow command:

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);

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

2)Validation Rules Array: In this second argument we have to pass array of validation rules.

3)Validation Rules Custom Message: In this third argument we have to pass array of custom message.

Ok, so you have to just copy bellow code and put it into your HomeController file.

i use unique validation rules, so make sure you have "users" table with email columns.

app/Http/Controllers/HomeController.php

<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;

use App\Http\Requests;


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 successfully added all fields.');

}

}

Step 3: Add Blade File

In Last step we have to create new form-validation.blade.php file and write code of bootstrap layout. In this file i write code for how to print error message code and how to get old value from input. I explain it as bellow:

1)$errors: we can get whole validation error from $errors variable. you can see bellow how can i display 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(): I use has() method of $errors object, that way i can check message is exist or not. It take single argument. If "firstname" field validation true then it will add "has-error" class will add, otherwise empty string. you can see as bellow:

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

3)first(): I use first() method of $errors object, that way i can get single message field wise. It take single argument. you can see as bellow:

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

4)old(): I use old() helper function of Laravel 5, this function will return old value of that field, when validation will false then you don't have to write other field again.

{{ old('firstname') }}

Ok, so now create new view file "form-validation.blade.php" and put bellow code on it.

resources/views/form-validation.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 5.3 - 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>

Ok, now you are ready to run and check in your laravel application. You can also get more pre-define validation from form here : Click Here.

Maybe it can help you....

Shares