Laravel Validation

Laravel Validation Class provide a convenient way for validating data and retrieving validation error messages. To Perform Laravel Validation.we will use following step.

Step 1

Create route for validation.

Route::any('laravelvalidation', array('as' => 'laravelvalidation', 
                      'uses' => '[email protected]'));
Step 2

Create view file inside app/views/validationexample.blade.php

<!doctype html>
<html>
<head>
    <title>Laravel Validation Example</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    <style>
        body {
            padding-bottom: 40px;
            padding-top: 40px;
        }
    </style>
</head>
<body class="container-fluid">
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<div class="page-header">
<h1>Laravel Validation Example By Tutsway.com</h1>
</div>
<form method="POST" action="/laravelvalidation" novalidate>
<div class="form-group @if ($errors->has('name')) has-error @endif">
<label for="name">Name*</label>
<input type="text" class="form-control" name="name" 
placeholder="Enter Your Name"> @if ($errors->has('name'))
<p class="help-block">{{ $errors->first('name') }}</p> @endif
</div>
<div class="form-group @if ($errors->has('email')) has-error @endif">
<label for="email">Email*</label>
<input type="text" class="form-control" name="email"
placeholder="Enter Your Email"> @if ($errors->has('email'))
<p class="help-block">{{ $errors->first('email') }}</p> @endif
</div>
<div class="form-group @if ($errors->has('password')) has-error @endif">
<label for="password">Password*</label>
<input type="password" class="form-control" name="password" placeholder="Enter Your Password"> @if ($errors->has('password'))
<p class="help-block">{{ $errors->first('password') }}</p> @endif
</div>
<div class="form-group @if ($errors->has('password_confirm')) has-error @endif">
<label for="password_confirm">Confirm Password*</label>
<input type="password" class="form-control" name="password_confirm" placeholder="Confirm Your Password"> @if ($errors->has('password_confirm'))
<p class="help-block">{{ $errors->first('password_confirm') }}</p> @endif
</div>
<input type="submit" name="submit" value="Submit" class="btn btn-success">
</form>
</div>
</div>
</body>
</html>
Step 3

Create Controller file file inside app/controllers/ValidationController.php

<?php
class ValidationController extends Controller {

public function index () {
   
    if(Input::get('submit'))
       {
$input = Input::only('name','email','lname','password','password_confirm');              
$rules = array('name'=> 'required',
        'email' => 'required|email',
        'password'=> 'required',
        'password_confirm' =>'required|same:password'
);                      
              
$validator = Validator::make($input, $rules);
if ($validator->fails()) {
// get the error messages from the validator
$messages = $validator->messages();
// redirect our user back to the form with the errors from the validator
return Redirect::to('laravelvalidation')->withErrors($validator);
} else {           
return Redirect::to('laravelvalidation');
} 
   }
return View::make('validationexample');
    }
   }

Available Laravel Validation Rules

Below is a list of Available Laravel validation rules.

Rule Description
Required Ensure that a value for a field is present.
Alpha Ensure that a value for a field contains only alphabetic characters.
AlphaNum Ensure that a value for a field contains only alphabetic and numeric characters.
AlphaDash Ensure that a value for a field contains only alpha-numeric characters,dashes,and underscores.
Image Ensure that uploaded file must be an image.
Url Ensure that an attribute is a URL.
Email Ensure that a value for a field must be a valid email address.
Email Ensure that a value for a field must be a valid email address.
Ip Ensure that a value for a field must be a valid Ip address.
ExistCount:users Get the number of records that exist in database.
Confirmed Ensure that a value for a field must match a confirmation field, named in the format _confirmation.
Integer Ensure that a value of a field must be an integer or whole number.
Numeric Ensure that a value of a field must be an numeric.
Size:7 Ensure that a value of a field must be exactly 7 characters long.
Between:5,10 Ensure that a value of a field must be between 5 and 10 characters.