Laravel 5.3 – Form Input Validation rules example with demo
In this post we will give you information about Laravel 5.3 – Form Input Validation rules example with demo. Hear we will give you detail about Laravel 5.3 – Form Input Validation rules example with demoAnd how to use it also give you demo for it if it is necessary.
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 AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpRequests;
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 >
<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 >
<h2>Form Validation</h2>
<form method="POST" action="/form-validation" autocomplete="off">
@if(count($errors))
<div >
<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 >
<div >
<div >
<label for="firstname">First Name:</label>
<input type="text" id="firstname" name="firstname" placeholder="Enter First Name" value="{{ old('firstname') }}">
<span >{{ $errors->first('firstname') }}</span>
</div>
</div>
<div >
<div >
<label for="lastname">Last Name:</label>
<input type="text" id="lastname" name="lastname" placeholder="Enter Last Name" value="{{ old('lastname') }}">
<span >{{ $errors->first('lastname') }}</span>
</div>
</div>
</div>
<div >
<div >
<div >
<label for="email">Email:</label>
<input type="text" id="email" name="email" placeholder="Enter Email" value="{{ old('email') }}">
<span >{{ $errors->first('email') }}</span>
</div>
</div>
<div >
<div >
<label for="mobileno">Mobile No:</label>
<input type="text" id="mobileno" name="mobileno" placeholder="Enter Mobile No" value="{{ old('mobileno') }}">
<span >{{ $errors->first('mobileno') }}</span>
</div>
</div>
</div>
<div >
<div >
<div >
<label for="password">Password:</label>
<input type="password" id="password" name="password" placeholder="Enter Password" >
<span >{{ $errors->first('password') }}</span>
</div>
</div>
<div >
<div >
<label for="confirm_password">Confirm Password:</label>
<input type="password" id="confirm_password" name="confirm_password" placeholder="Enter Confirm Passowrd">
<span >{{ $errors->first('confirm_password') }}</span>
</div>
</div>
</div>
<div >
<div >
<div >
<label for="details">Details:</label>
<textarea name="details" id="details" placeholder="Enter Details">{{ old('details') }}</textarea>
<span >{{ $errors->first('details') }}</span>
</div>
</div>
</div>
<div >
<button >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….
Hope this code and post will helped you for implement Laravel 5.3 – Form Input Validation rules example with demo. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs