Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php namespace App\Providers;
use Illuminate\Contracts\Validation\Factory;
use Illuminate\Validation\ValidationServiceProvider as ServiceProvider;
class ValidationServiceProvider extends ServiceProvider
{
protected $extendValidators = [
//some validators name there
];
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
parent::register();
$this->extendValidation();
}
/**
* Extend validator factory.
*/
protected function extendValidation()
{
$provider = $this;
$this->app->extend('validator', function (Factory $validator) use ($provider) {
foreach ($provider->extendValidators as $validatorName) {
$validator->extend($validatorName, $provider->getValidatorCallback($validatorName));
}
return $validator;
});
}
/**
* Get validator function.
*
* @param string $validatorName
* @return callable
*/
public function getValidatorCallback(array $validatorName)
{
$validator = [$this, camel_case('validate_' . $validatorName)]
return function () use ($validator) {
return call_user_func_array($validator, func_get_args());
};
}
//some validator functions there
//public function validateSome($attribute, $values, $parameters){}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.