Created
August 20, 2013 17:35
-
-
Save tournasdim/6284629 to your computer and use it in GitHub Desktop.
L4 Validation examples (basic , how to define custom Validation rules )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Signature of a Validation */ | |
$validator = Validator::make($data , $rules , $customMessage) ; | |
/* Validating submitted fields */ | |
$inputs = Input::all(); | |
$rules = array( | |
'name' => 'Required|max:100', | |
'email' => 'Required|email|max:100', | |
'msg' => 'Required|max:500' | |
); | |
$validator = Validator::make($inputs, $rules); | |
// | |
/* Rules as "|" separator */ | |
$validator = Validator::make( ['name' => 'Dayle' ] , ['name' => 'required|min:5' ]) ; | |
/* Rules as array */ | |
$validator = Validator::make( ['name' => 'Dayle'] , ['name' => ['required', 'min:5']]) ; | |
if ($validator->fails()) { /* do stuff here */ } | |
/* Passing cusotm message to the Validator */ | |
$messages = array('required' => 'The :attribute field is required.', ); | |
/* Passing cusotm messages to the Validator */ | |
$messages = array( | |
'same' => 'The :attribute and :other must match.', | |
'size' => 'The :attribute must be exactly :size.', | |
'between' => 'The :attribute must be between :min - :max.', | |
'in' => 'The :attribute must be one of the following types: :values', | |
); | |
/* Specifying A Custom Message For A Given Attribute */ | |
$messages = array('email.required' => 'We need to know your e-mail address!',); | |
$validator = Validator::make($input, $rules, $messages); | |
/* Registering A Custom Validation Rule as a Closure */ | |
Validator::extend('foo', function($attribute, $value, $parameters) | |
{ | |
return $value == 'foo'; | |
}) ; | |
/* Registering A Custom Validation Rule as a Class-Method */ | |
Validator::extend('foo', 'FooValidator@validate'); | |
/* Extending The Validator Class */ | |
class CustomValidator extends Illuminate\Validation\Validator { | |
public function validateFoo($attribute, $value, $parameters) | |
{ | |
return $value == 'foo'; | |
} | |
} | |
Validator::resolver(function($translator, $data, $rules, $messages) | |
{ | |
return new CustomValidator($translator, $data, $rules, $messages); | |
}); | |
##################################################################### | |
########## Practical example ############## | |
## Registering a Custom rule into a Closure ############## | |
##################################################################### | |
1) Into the routes.php or app/start/global.php file | |
Validator::extend('divisible_by_two', function($attribute, $value, $parameters) | |
{ | |
return ($value % 2) === 0; | |
}); | |
2) // Routes.php file | |
Route::get('divide/{number}', function($number) | |
{ | |
$input = ['number' => $number]; | |
$rule = ['number' => 'divisible_by_two']; | |
$messages = ['divisible_by_two' => 'The :attribute must be divisible by two.']; | |
$validator = Validator::make($input, $rule, $messages); | |
if ($validator->fails()) { | |
return $validator->messages()->first('number'); | |
} | |
return $number . ' is divisible by two.'; | |
}); | |
##################################################################### | |
########## Practical example ############## | |
## Registering a Custom rule into a Custom Class ############## | |
##################################################################### | |
1) Register a custom Library folder (app/lib) | |
2) Create a class into app/lib/tournasdim/validator/Divisible.php | |
<?php namespace Tournasdim\Validator; | |
class Divisible | |
{ | |
public function divisibleByTwo($attribute, $value, $parameters) | |
{ | |
return ($value % 2) === 0; | |
} | |
} | |
3) Run a "composer dump-autoload" | |
4) Into the app/start/global.php file register the Validator | |
Validator::extend('divisible_by_two', '\Tournasdim\Validator\Divisible@divisibleByTwo'); | |
5) // Routes.php file | |
Route::get('divide/{number}', function($number) | |
{ | |
$input = ['number' => $number]; | |
$rule = ['number' => 'divisible_by_two']; | |
$messages = ['divisible_by_two' => 'The :attribute must be divisible by two.']; | |
$validator = Validator::make($input, $rule, $messages); | |
if ($validator->fails()) { | |
return $validator->messages()->first('number'); | |
} | |
return $number . ' is divisible by two.'; | |
}); | |
##################################################################### | |
########## Practical example ############### | |
## Registering a Custom rule by extending the Validator Class ##### | |
##################################################################### | |
1) Register a custom Library folder (app/lib) | |
2) Create a class into app/lib/tournasdim/validator/Divisible.php | |
<?php namespace Tournasdim\Validator ; | |
class Divisible extends \Illuminate\Validation\Validator | |
{ | |
public function validateDivisibleByTwo($attribute, $value, $parameters) | |
{ | |
return ($value % 2) === 0; | |
} | |
} | |
3) Run a "composer dump-autoload" | |
4) Into the app/start/global.php file register the Validator | |
Validator::resolver(function($translator, $data, $rules, $messages) | |
{ | |
return new \Tournasdim\Validator\Divisible($translator, $data, $rules, $messages); | |
}); | |
5) // Routes.php file | |
Route::get('divide/{number}', function($number) | |
{ | |
$input = ['number' => $number]; | |
$rule = ['number' => 'divisible_by_two']; | |
$messages = ['divisible_by_two' => 'The :attribute must be divisible by two.']; | |
$validator = Validator::make($input, $rule, $messages); | |
if ($validator->fails()) { | |
return $validator->messages()->first('number'); | |
} | |
return $number . ' is divisible by two.'; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment