Friday, March 22, 2013

Create a Custom Validation Rules in Laravel 3


1.Open Start.php in the application folder and add a path map to your class that you are going to use in validation

Autoloader::map(array(

 'customValidator' => __DIR__.DS.'libraries'.DS.'Cvalidator.php',

));






in this code,   the name of my custom validator class is: 'Cvalidator.php'
and reside in  the directory : application/libraries/
Now, The system will know the place of your class .
2. Also in the smae file 'start.php' register your new validation rule as follows:


Laravel\Validator::register('customrule', function($attribute, $value, $parameters)

{

 return customValidator::CheckThis($attribute, $value, $parameters);

});




3.Next you need to create your class with methodes that will handle the validation staff. but assure that you put in the right path that you map it in start.php, here an example: 

Don't Extend the Laravel Validation Class



<?php class Cvalidator{ public static

 CheckThis(){ 

 //do the validation heare then: return true or false only return true;
}

No comments:

Post a Comment