Using of 40 different validation rules in Laravel

 

In my earlier article i had discussed how to use form validation rules in laravel. Today i am going to discuss about laravel validation rules. Before we start, the most important thing is “what is validation rules”? Validation rules prevent the invalid data from user input and validates the data with proper validation allow the filter data. If validation rules found eny errors then it returns all error message and display it to the user, so that user can knows what is the exact issue. Laravel has lots of inbuilt validation rules like validate blank field, validate numeric data, validate email address and many more. From this article you can get an idea how to use the inbuilt validation rules in laravel.

Routes:
Define your routes in app/Http/routes.php file.

//This will display the form
Route::get('add_validation', 'TestController@add_validation');
 
//Validate the form
Route::post('validate_form', 'TestController@validate_form');

Below are the TestController controller file.

<?php 
namespace App\Http\Controllers;
use Illuminate\Http\Request; 
use App\Http\Controllers\Controller; 
 
class TestController extends Controller {
	
	public function add_validation(Request $request){
		return view('add_validation'); 		
	}
	
	public function validate_form(Request $request){
		if ($request->isMethod('post')){
                    $terms=$request->input('terms');
		    $url=$request->input('url');			
		    $after_dt=$request->input('after_dt');
	            $alpha=$request->input('alpha');
	            $alpha_dash=$request->input('alpha_dash');
		    $alpha_numeric=$request->input('alpha_numeric');
                
		    $in_ans1=$request->input('in_ans1');
		    $in_ans2=$request->input('in_ans2');
		    $in_ans3=$request->input('in_ans3');
				
		    $regex_fld=$request->input('regex_fld');
							
		$validator = Validator::make($request->all(), [
			'terms' => 'accepted',
			'url' => 'active_url',
			'after_dt' => 'after:'.date('Y-m-d'),
			'alpha' => 'alpha',
			'alpha_dash' => 'alpha_dash',
			'alpha_numeric' => 'alpha_num',
		        'arr_chkbox' => 'array',
			'before_dt' => 'before:'.date('Y-m-d'),
			'between_fld' => 'between:1,3', //Count length of text
			'boolean_fld' => 'boolean',
			'password' => 'required',
			'password_confirmation' => 'confirmed',
			'date_fld' => 'date',
			'date_format_fld' => 'date_format:Y-m-d',
			'different_fld' => 'different:date_format_fld', 
                        'digits_fld' => 'digits:2',
			'digits_between_fld' => 'digits_between:2,3',
			'email_fld' => 'email',
			'exists_fld' => 'exists:lesson,title,lesson_id,1',
			'img_fld' => 'image',
			'in_ans1' => 'required',
			'in_ans2' => 'required',
			'in_ans3' => 'required',
			'in_correct_ans' => 'in:'.$in_ans1.','.$in_ans2.','.$in_ans3,
			'not_in_correct_ans' => 'not_in:'.$in_ans1.','.$in_ans2.','.$in_ans3,
			'integer_fld'=>'integer',
			'ip_fld'=>'ip',
			'max_value_fld'=>'max:3',
			'mimes_fld'=>'mimes:jpeg,jpg,doc,bmp,png',
			'min_value_fld'=>'min:2',
			'numeric_fld'=>'numeric',
			'regex_fld'=>'regex:/^\d{4}$/',
			'required_if_fld'=>'required_if:regex_fld,'.date('Y'),
			'required_with_fld'=>'required_with:regex_fld,numeric_fld',
			'required_with_all_fld'=>'required_with_all:regex_fld,numeric_fld',
			'required_without_fld'=>'required_without:regex_fld,numeric_fld',
			'required_without_all_fld'=>'required_without_all:regex_fld,numeric_fld',
			'same_fld'=>'same:numeric_fld',
			'size_fld'=>'size:3',
			'string_fld'=>'string',
			'timezone_fld'=>'timezone',
			'unique_fld' => 'unique:user,username,NULL,id',
			'url_fld' => 'url'
			]);
					
			if ($validator->fails()) {					   
			    return redirect("validate_form")
		                   ->withErrors($validator)
				   ->withInput();				   	  
		        }				
		}	  
	}
}
?>

Create a blade template file under “resources/views/add_validation.blade.php”

<div class="box-body"> {!! Form::open(array('url' =>'validate_form', 'class' => '' ,'method' => 'post','files'=>true)) !!}
  <div class="box-body">
    <div class="form-group" style="width: 50%; float: left; margin: 2px;">
      {!!Form::checkbox('terms','1',Request::old('terms'))!!}
	  <label for="Title">I accept terms and conditions</label>	  
      @if ($errors->has('terms')){!! '<span class="text-red">'.$errors->first('terms').'</span>' !!} @endif </div>
	 <div style="clear:both"></div>
    <div class="form-group"  style="width: 18%; float: left; margin: 2px;">
      <label for="Title">Active URL:</label>
      {!!Form::text('url',Request::old('url'),['placeholder'=>'Please enter active URL','class'=>'form-control'])!!}	  
      @if ($errors->has('url')){!! '<span class="text-red">'.$errors->first('url').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">After Date:</label>
      {!!Form::text('after_dt',Request::old('after_dt'),['placeholder'=>'Please enter date','class'=>'form-control'])!!}	  
      @if ($errors->has('after_dt')){!! '<span class="text-red">'.$errors->first('after_dt').'</span>' !!} @endif
	</div>	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Alpha:</label>
      {!!Form::text('alpha',Request::old('alpha'),['placeholder'=>'Please enter alpha','class'=>'form-control'])!!}	  
      @if ($errors->has('alpha')){!! '<span class="text-red">'.$errors->first('alpha').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Alpha Dash:</label>
      {!!Form::text('alpha_dash',Request::old('alpha_dash'),['placeholder'=>'Please enter alpha dash','class'=>'form-control'])!!}	  
      @if ($errors->has('alpha_dash')){!! '<span class="text-red">'.$errors->first('alpha_dash').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Alpha Numeric:</label>
      {!!Form::text('alpha_numeric',Request::old('alpha_numeric'),['placeholder'=>'Please enter alpha numeric','class'=>'form-control'])!!}	  
      @if ($errors->has('alpha_numeric')){!! '<span class="text-red">'.$errors->first('alpha_numeric').'</span>' !!} @endif
	</div>
	 
    <div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Array:</label>
	  {!!Form::checkbox('arr_chkbox[]','php',Request::old('arr_chkbox[]'))!!}
	  <label for="Title">PHP</label>
	  {!!Form::checkbox('arr_chkbox[]','javascript',Request::old('arr_chkbox[]'))!!}
	  <label for="Title">Javascript</label>	  
      @if ($errors->has('arr_chkbox')){!! '<span class="text-red">'.$errors->first('arr_chkbox').'</span>' !!} @endif
	</div>
    
	<div class="form-group"  style="width: 18%; float: left; margin: 2px;">
      <label for="Title">Before Date:</label>
      {!!Form::text('before_dt',Request::old('before_dt'),['placeholder'=>'Please enter after date','class'=>'form-control'])!!}	  
      @if ($errors->has('before_dt')){!! '<span class="text-red">'.$errors->first('before_dt').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Between:</label>
      {!!Form::text('between_fld',Request::old('between_fld'),['placeholder'=>'Please enter between text','class'=>'form-control'])!!}	  
      @if ($errors->has('between_fld')){!! '<span class="text-red">'.$errors->first('between_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Boolean:</label><br />
      {!!Form::radio('boolean_fld','1',Request::old('boolean_fld'))!!}
	  <label for="Title">True</label>
	  {!!Form::radio('boolean_fld','0',Request::old('boolean_fld'))!!}
	  <label for="Title">False</label>
	  {!!Form::radio('boolean_fld','2',Request::old('boolean_fld'))!!}
	  <label for="Title">Other</label>
	  <br />
      @if ($errors->has('boolean_fld')){!! '<span class="text-red">'.$errors->first('boolean_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Password:</label>
      {!!Form::password('password',['placeholder'=>'Please enter password','class'=>'form-control'])!!}	  
      @if ($errors->has('password')){!! '<span class="text-red">'.$errors->first('password').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Confirm Password:</label>
      {!!Form::password('password_confirmation',['placeholder'=>'Please enter confirm password','class'=>'form-control'])!!}	  
      @if ($errors->has('password_confirmation')){!! '<span class="text-red">'.$errors->first('password_confirmation').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 18%; float: left; margin: 2px;">
      <label for="Title">Date:</label>
      {!!Form::text('date_fld',Request::old('date_fld'),['placeholder'=>'Please enter date','class'=>'form-control'])!!}	  
      @if ($errors->has('date_fld')){!! '<span class="text-red">'.$errors->first('date_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 26%; float: left; margin: 2px;">
      <label for="Title">Date Format:</label>
      {!!Form::text('date_format_fld',Request::old('date_format_fld'),['placeholder'=>'Please enter date format as YYYY-mm-dd','class'=>'form-control'])!!}	  
      @if ($errors->has('date_format_fld')){!! '<span class="text-red">'.$errors->first('date_format_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 19%; float: left; margin: 2px;">
      <label for="Title">Different:</label>
      {!!Form::text('different_fld',Request::old('different_fld'),['placeholder'=>'Please enter different','class'=>'form-control'])!!}	  
      @if ($errors->has('different_fld')){!! '<span class="text-red">'.$errors->first('different_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 15%; float: left; margin: 2px;">
      <label for="Title">Digits:</label>
      {!!Form::text('digits_fld',Request::old('digits_fld'),['placeholder'=>'Please enter digits','class'=>'form-control'])!!}	  
      @if ($errors->has('digits_fld')){!! '<span class="text-red">'.$errors->first('digits_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Digits Between:</label>
      {!!Form::text('digits_between_fld',Request::old('digits_between_fld'),['placeholder'=>'Please enter digits between','class'=>'form-control'])!!}	  
      @if ($errors->has('digits_between_fld')){!! '<span class="text-red">'.$errors->first('digits_between_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Email:</label>
      {!!Form::text('email_fld',Request::old('email_fld'),['placeholder'=>'Please enter email','class'=>'form-control'])!!}	  
      @if ($errors->has('email_fld')){!! '<span class="text-red">'.$errors->first('email_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Exists:</label>
      {!!Form::text('exists_fld',Request::old('exists_fld'),['placeholder'=>'Please enter exists','class'=>'form-control'])!!}	  
      @if ($errors->has('exists_fld')){!! '<span class="text-red">'.$errors->first('exists_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Image:</label>
      {!!Form::file('img_fld')!!}
      @if ($errors->has('img_fld')){!! '<span class="text-red">'.$errors->first('img_fld').'</span>' !!} @endif
	</div>
	<div style="clear:both;"></div>
	<div class="form-group"  style="width: 100%; float: left; margin: 2px;">
      <label for="Title">In:</label>
	  <br />
	  2+2=_____
	  <br />
      <div>
      <span style=" float:left; padding:6px 7px 0 0;">&nbsp;a)</span>
      <span style="float:left;">{!!Form::text('in_ans1',Request::old('in_ans1'),['placeholder'=>'Please enter answer1','class'=>'form-control'])!!}</span>
      </div>
	  <div>
      @if ($errors->has('in_ans1')){!! '<span class="text-red">'.$errors->first('in_ans1').'</span>' !!} @endif
      </div>
	 
      <div>
      <span style=" float:left; padding:6px 7px 0 0;">&nbsp;b)</span><span
      style="float:left;">{!!Form::text('in_ans2',Request::old('in_ans2'),['placeholder'=>'Please enter answer2','class'=>'form-control'])!!}</span>
      </div>
	  <div>
      @if ($errors->has('in_ans2')){!! '<span class="text-red">'.$errors->first('in_ans2').'</span>' !!} @endif
      </div>
	  <div style="padding:2px;"></div>
      
      <div>
      <span style=" float:left; padding:6px 7px 0 0;">&nbsp;c)</span>
      <span style="float:left;">{!!Form::text('in_ans3',Request::old('in_ans3'),['placeholder'=>'Please enter answer3','class'=>'form-control'])!!}</span>
      </div>
	  <div>
      @if ($errors->has('in_ans3')){!! '<span class="text-red">'.$errors->first('in_ans3').'</span>' !!} @endif
      </div>
	  
      <div>
      <span>&nbsp;</span>
      <span style="float: left; padding-left: 23px; width: 220px;">{!!Form::text('in_correct_ans',Request::old('in_correct_ans'),['placeholder'=>'Please enter correct answer','class'=>'form-control'])!!}</span>
      </div>
	  <div>
      @if ($errors->has('in_correct_ans')){!! '<span class="text-red">'.$errors->first('in_correct_ans').'</span>' !!} @endif
      </div>	  
	</div>
	
	<div class="form-group"  style="width: 18%; float: left; margin: 2px;">
      <label for="Title">Not In:</label>
      {!!Form::text('not_in_correct_ans',Request::old('not_in_correct_ans'),['placeholder'=>'Please enter Not in','class'=>'form-control'])!!}
	  @if ($errors->has('not_in_correct_ans')){!! '<span class="text-red">'.$errors->first('not_in_correct_ans').'</span>' !!} @endif	  
	</div>
	
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Integer:</label>
      {!!Form::text('integer_fld',Request::old('integer_fld'),['placeholder'=>'Please enter integer value','class'=>'form-control'])!!}	  
      @if ($errors->has('integer_fld')){!! '<span class="text-red">'.$errors->first('integer_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">IP:</label>
      {!!Form::text('ip_fld',Request::old('ip_fld'),['placeholder'=>'Please enter IP Address','class'=>'form-control'])!!}	  
      @if ($errors->has('ip_fld')){!! '<span class="text-red">'.$errors->first('ip_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Max Value:</label>
      {!!Form::text('max_value_fld',Request::old('max_value_fld'),['placeholder'=>'Please enter max value','class'=>'form-control'])!!}	  
      @if ($errors->has('max_value_fld')){!! '<span class="text-red">'.$errors->first('max_value_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Mimes Files:</label>
      {!!Form::file('mimes_fld')!!}
      @if ($errors->has('mimes_fld')){!! '<span class="text-red">'.$errors->first('mimes_fld').'</span>' !!} @endif
	</div>
	
	<div style="clear:both;"></div>
	<div class="form-group"  style="width: 18%; float: left; margin: 2px;">
      <label for="Title">Min Value:</label>
      {!!Form::text('min_value_fld',Request::old('min_value_fld'),['placeholder'=>'Please enter min value','class'=>'form-control'])!!}	  
      @if ($errors->has('min_value_fld')){!! '<span class="text-red">'.$errors->first('min_value_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Numeric:</label>
      {!!Form::text('numeric_fld',Request::old('numeric_fld'),['placeholder'=>'Please enter numeric value','class'=>'form-control'])!!}	  
      @if ($errors->has('numeric_fld')){!! '<span class="text-red">'.$errors->first('numeric_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Regex:</label>
      {!!Form::text('regex_fld',Request::old('regex_fld'),['placeholder'=>'Please enter regex value','class'=>'form-control'])!!}	  
      @if ($errors->has('regex_fld')){!! '<span class="text-red">'.$errors->first('regex_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Required If:</label>
      {!!Form::text('required_if_fld',Request::old('required_if_fld'),['placeholder'=>'Please enter if value','class'=>'form-control'])!!}	  
      @if ($errors->has('required_if_fld')){!! '<span class="text-red">'.$errors->first('required_if_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Required With:</label>
      {!!Form::text('required_with_fld',Request::old('required_with_fld'),['placeholder'=>'Please enter with value','class'=>'form-control'])!!}	  
      @if ($errors->has('required_with_fld')){!! '<span class="text-red">'.$errors->first('required_with_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Required With All:</label>
      {!!Form::text('required_with_all_fld',Request::old('required_with_all_fld'),['placeholder'=>'Please enter with all value','class'=>'form-control'])!!}	  
      @if ($errors->has('required_with_all_fld')){!! '<span class="text-red">'.$errors->first('required_with_all_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Required Without:</label>
      {!!Form::text('required_without_fld',Request::old('required_without_fld'),['placeholder'=>'Please enter without value','class'=>'form-control'])!!}	  
      @if ($errors->has('required_without_fld')){!! '<span class="text-red">'.$errors->first('required_without_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Required Without All:</label>
      {!!Form::text('required_without_all_fld',Request::old('required_without_all_fld'),['placeholder'=>'Please enter without all value','class'=>'form-control'])!!}	  
      @if ($errors->has('required_without_all_fld')){!! '<span class="text-red">'.$errors->first('required_without_all_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Same:</label>
      {!!Form::text('same_fld',Request::old('same_fld'),['placeholder'=>'Please enter same value','class'=>'form-control'])!!}	  
      @if ($errors->has('same_fld')){!! '<span class="text-red">'.$errors->first('same_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 18%; float: left; margin: 2px;">
      <label for="Title">Size:</label>
      {!!Form::text('size_fld',Request::old('size_fld'),['placeholder'=>'Please enter size value','class'=>'form-control'])!!}	  
      @if ($errors->has('size_fld')){!! '<span class="text-red">'.$errors->first('size_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">String:</label>
      {!!Form::text('string_fld',Request::old('string_fld'),['placeholder'=>'Please enter string value','class'=>'form-control'])!!}	  
      @if ($errors->has('string_fld')){!! '<span class="text-red">'.$errors->first('string_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Timezone:</label>
      {!!Form::text('timezone_fld',Request::old('timezone_fld'),['placeholder'=>'Please enter timezone value','class'=>'form-control'])!!}	  
      @if ($errors->has('timezone_fld')){!! '<span class="text-red">'.$errors->first('timezone_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">Unique:</label>
      {!!Form::text('unique_fld',Request::old('unique_fld'),['placeholder'=>'Please enter unique value','class'=>'form-control'])!!}	  
      @if ($errors->has('unique_fld')){!! '<span class="text-red">'.$errors->first('unique_fld').'</span>' !!} @endif
	</div>
	
	<div class="form-group"  style="width: 20%; float: left; margin: 2px;">
      <label for="Title">URL:</label>
      {!!Form::text('url_fld',Request::old('url_fld'),['placeholder'=>'Please enter url','class'=>'form-control'])!!}	  
      @if ($errors->has('url_fld')){!! '<span class="text-red">'.$errors->first('url_fld').'</span>' !!} @endif
	</div>
  </div>
  <!-- /.box-body -->
  <div class="box-footer" style="text-align:center;">
    <button type="submit" class="btn btn-primary">Submit</button>
     </div>
  {!! Form::close() !!} </div>

This is the image which will generate the form from above template file.

Validation Rules in Laravel

For your convenience i have given all input box a number. As per above image we will discuss all validation methods of laravel which will easy for you to understand.

1)accepted
This is mainly used for validating Terms & Conditions. The value must be yes,on,true or 1

2)active_url
This functions validate the URL and checks whether the website is active or not using PHP checkdnsrr function
Valid URL: http://www.google.com,http://www.facebook.com
Invalid URL : http://www.asdkasd.com,http://www.bczxczxcz.com

3)after:date
It validates the date and value must be after the given date. The dates value will be passed into the PHP strtotime function.
Example :
‘after_dt’ => ‘required|after:’.date(‘Y-m-d’),
In the above code the validation will check given date must be after today’s date

4)alpha
The given value must be alphabetic characters. If you have pass any digits or symbol then it throws an error.
Valid Value: abc,xyz

5)alpha_dash
Under this validation, this method checks given value may contain letters,numbers and dashes and underscore.
Valid Value: a4sb-5_,1234,xyz

6)alpha_num
The field under this validation may be contain letters and numbers only.

7)array
The field under this validation must be an array.

8)before:date
It validates the date and value must be preceding the given date. The dates value will be passed into the PHP strtotime function.
Example :
‘after_dt’ => ‘required|after:’.date(‘Y-m-d’),
In the above code the validation will check given date must be before today’s date

9)between:min,max
The field under this validation must have a length between the given min and max size.
Example :’between_fld’ => ‘between:1,3’

In the above code the input field between_fld must have a minimum length of 1 and maximum length of 3

Valid Values: 1,R5,XyZ

10)boolean
The field under this validation must be a boolean value. Accepted values are true, false, 1, 0, “1”, and “0”.

11)required
It validates the input data and fields must not be blank.

12)confirmed
The field under this validation methods must have a matching field of password_confirmation.
For example, if there is a field name as password, a password_confirmation field must be exist in the input.

13)date
The field under this validation must be a valid date.
Valid Values: 20160101,2016/01/01,01/01/2016

14)date_format:format
The field under this validation must match the given date format. This method use the PHP’s date_parse_from_format function to evaluated. You should use either date or date_format when validating a date field.
Example: ‘date_format_fld’ => ‘required|date_format:Y-m-d’

In the above example the field accepts only this format 2016-01-01 rather than 01-02-2016

14)different:field
If you want to validate a field which must have a different value from the given field then this is the correct function.
In the above image serial no 15 input value should not be same as input value of serial no 14

16)digits:value
If you want to validate your input field will accept only numeric value and must have an exact length of value, then you can use this method.
Example: ‘digits_fld’ => ‘digits:2’,
Valid values:34

In the above code it accepts only numeric value and length must be 2

17)digits_between:min,max
If you want to validate any numeric value which must have a length between minumum and maximum then you can use this method.
Example: ‘digits_between_fld’ => ‘digits_between:2,3’

In the above code the digits_between_fld input field will accept minimum length of 2 and maximum length of 3 digits.
Valid values: 23,456

18)email
It validates an e-mail address.

19)exists:table,column
If you want to check the input value which must be exist on a given table then you can use this method.See the below example.
Example: ‘exists_fld’ => ‘exists:lesson,title’

In the above code checks from the lesson table whose title must be the input value of “exists_fld”.

If you want to add more “AND” condition then you can write the code like:
Example: ‘exists_fld’ => ‘exists:lesson,title,lesson_id,’.$request->input(‘in_ans3′).’

Above code will genere the sql query like :
select count(*) as aggregate from `lesson` where `title` =’‘ and `lesson_id` =’

20)image
If you want to validate your input field must be an image then you can use this method. Valid image should be (jpeg, png, bmp, gif, or svg)

21)in:value1,value,…
If you you want to validate a field which must be included in the given list of values, then you can use this function.

22)not_in:value1,value,…
If you you want to validate a field which must not be included in the given list of values, then you can use this function. This is the reverse of in method

23)integer
Under this validation method, the value must be an integer.

24)ip
The field under this validation must be an IP address.

25)max:value
If you want to validates a input field accepts a size of maximum or equal value then you can use this function.

Example: ‘max_value_fld’=>’max:3’

In the above code the max_value_fld field value accepts a maximum or equal to 3 length.
Valid Example :ab,123,Z

26)mimes:jpeg,png,…
If you want to validate an input field which accepts only MIME extension then use this method.

Example: ‘mimes_fld’=>’mimes:jpeg,jpg,doc,bmp,png’,

In the above code the input field mimes_fld accepts only jpeg,jpg,doc,bmp,png MIME type.

27)min:value
The field under this validation must have a minimum value in size.
Example:’min_value_fld’=>’min:2′

In the above code, the input field min_value_fld must accept a value of 2 in size
Valid Values:23,4h

28)numeric
The field under this validation must be numeric.

29)regex:pattern
If you want to validate your input field with regular expression then this method must match the given regular expression.
Example : ‘regex_fld’=>’required|regex:/^\d{4}$/’

In the above example the input field regex_fld must be an integer value with 4 digits.
Valid Values: 1234,5683

30)required_if:anotherfield,value,…
If you want to validate your input field, that must accepts the value where another field is equal to any value then you can use this method.
Example: ‘regex_fld’=>’regex:/^\d{4}$/’,
‘required_if_fld’=>’required_if:regex_fld,’.date(‘Y’)

In the above code when you enter current year in the regex_fld input field then you must enter the value in required_if_fld input field. Because required_if_fld input field is required when you enter current year in the regex_fld field.

31)required_with:value1,value2,…
If you want to validate any input field that must be present only if any of the other specified fields are present. Then you can use this method.

Example: ‘numeric_fld’=>’numeric’,
‘regex_fld’=>’regex:/^\d{4}$/’,
‘required_with_fld’=>’required_with:regex_fld,numeric_fld’

In the above code the input field required_with_fld must not be empty if value exist in any of the numeric_fld or regex_fld input field.

32)required_with_all:value1,value2,…
You can use this function if you want to validate the input field that must be present only if all of the other specified fields are present.

Example: ‘numeric_fld’=>’numeric’,
‘regex_fld’=>’regex:/^\d{4}$/’,
‘required_with_all_fld’=>’required_with_all:regex_fld,numeric_fld’

In the above code the input field required_with_all_fld must not be empty if input field of numeric_fld and regex_fld is not empty.

33)required_without:value1,value2,…
You can use this function if you want to validate the input field that must be present only when any of the other specified fields are not present.

Example: ‘numeric_fld’=>’numeric’,
‘regex_fld’=>’regex:/^\d{4}$/’,
‘required_without_fld’=>’required_without:regex_fld,numeric_fld’

In the above code the input field required_without_fld must not be empty if value of either numeric_fld or regex_fld input field are not present.

34)required_without_all:value1,value2,…
You can use this function if you want to validate the input field that must be present only when all of the other specified fields are not present. This is the slight modification of required_without method.

Example: ‘numeric_fld’=>’numeric’,
‘regex_fld’=>’regex:/^\d{4}$/’,
‘required_without_all_fld’=>’required_without_all:regex_fld,numeric_fld’

In the above code the input field required_without_all_fld must not be empty if value of numeric_fld and regex_fld input field are not present.

35)same:field
If you want to validate an input field that must be same value with given field, then use this method.
Example: ‘numeric_fld’=>’numeric’,
‘same_fld’=>’same:numeric_fld’

In the above code if numeric_fld input value is not equal with same_fld input field then it throw an error.

36)size:value
If you want to validate size of any input field that must have a matching of given value then this is the method. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For files, size corresponds to the file size in kilobytes.

Example: ‘size_fld’=>’size:3’

The input field size_fld must have a size of 3

37)string
If you want to validate your input field that must accepts only string then use this method.
Example: ‘string_fld’=>’string’

38)timezone
If you want to validate any input field that must be a valid timezone identifier then use this method. Id valids the timezone according to according to the timezone_identifiers_list PHP function.

Example: ‘timezone_fld’=>’timezone’

Valid Values: Asia/Calcutta,NZ,Etc/UTC

39)unique:table,column,except,idColumn
If you want to validate any input fields value that must be unique with database table then you can use this method. If the column option is not specified, the field name will be used. This is the most suitable for validate whether email is exists or not on a table.

below are the example of different types of query :

Example 1 :’unique_fld’ => ‘unique:user,username,NULL,id,course_id,’.$request->input(‘course_id’).’,lesson_id,’.$request->input(‘course_id’).”

Above code will generate below sql code:
select count(*) as aggregate from `user` where `username` = and `course_id` = and `lesson_id` =

Example 2: ‘unique_fld’ => ‘unique:user’

SQL Code: select count(*) as aggregate from `user` where `unique_fld` =

Example 3: ‘unique_fld’ => ‘unique:user,username’

SQL Code: select count(*) as aggregate from `user` where `username` =

Example 4: ‘unique_fld’ => ‘unique:user,username,’.$request->input(‘course_id’).’,lesson_id’,

SQL Code : select count(*) as aggregate from `user` where `username` = and `lesson_id` <>

40)url
If you want to validate URL that must be a valid URL then use this method. This method validate the URL according to PHP’s filter_var function.

Leave a comment

SUBSCRIBE TO NEWSLETTER

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Categories