<?php
namespace Illuminate\Support\Facades;
/**
* @see \Illuminate\Validation\Factory
*/
class Validator extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'validator';
}
}
<?php
namespace Illuminate\Foundation;
...
class Application extends Container implements ApplicationContract, HttpKernelInterface
{
...
public function registerCoreContainerAliases()
{
foreach ([
...
'validator'=> [
\Illuminate\Validation\Factory::class,
\Illuminate\Contracts\Validation\Factory::class
],
])
...
}
...
}
//\Illuminate\Contracts\Validation\Factory 源码
protected function resolve(array $data, array $rules, array $messages, array $customAttributes)
{
if (is_null($this->resolver)) {
return new Validator(
$this->translator,
$data,
$rules,
$messages,
$customAttributes
);
}
return call_user_func(
$this->resolver,
$this->translator,
$data,
$rules,
$messages,
$customAttributes
);
}
Validator::resolver(function($translator, $data, $rules, $messages, $customAttributes){
return new ExtendValidator($translator, $data, $rules, $messages, $customAttributes);
});
//这是一个简单的参数比较的验证规则,Laravel5.8中提供,Laravel5.5中未提供
//验证规则如下: 'max_num'=>'gte:min',
Validator::extend('gte',function($attribute, $value, $parameters, $validator){
if($value>=data_get($validator->getData(),$parameters[0]))
{
return true;
}
return false;
});
//\Illuminate\Contracts\Validation\Factory 源码
public function extend($rule, $extension, $message = null)
{
$this->extensions[$rule] = $extension;
if ($message) {
$this->fallbackMessages[Str::snake($rule)] = $message;
}
}
//\Illuminate\Validation\Validator 源码
protected function callExtension($rule, $parameters)
{
$callback = $this->extensions[$rule];
if (is_callable($callback)) {
return call_user_func_array($callback, $parameters);
} elseif (is_string($callback)) {
return $this->callClassBasedExtension($callback, $parameters);
}
}
protected function validateAttribute($attribute, $rule)
{
...
$method = "validate{$rule}";
if ($validatable && ! $this->$method($attribute, $value, $parameters, $this)) {
$this->addFailure($attribute, $rule, $parameters);
}
}
public function __call($method, $parameters)
{
$rule = Str::snake(substr($method, 8));
if (isset($this->extensions[$rule])) {
return $this->callExtension($rule, $parameters);
}
throw new BadMethodCallException(sprintf(
'Method %s::%s does not exist.', static::class, $method
));
}
<?php
namespace Illuminate\Contracts\Validation;
interface Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value);
/**
* Get the validation error message.
*
* @return string
*/
public function message();
}
protected function validateUsingCustomRule($attribute, $value, $rule)
{
if(method_exists($rule, 'setValidator'))
{
$rule->setValidator($this);
}
return parent::validateUsingCustomRule($attribute,$value,$rule);
}
['min_num'=>'validateMinNum']方法1 通过自定义类实现 Laravel提供了
$rule = [方法2 通过extend方式实现
'min'=>new ClosureValidationRule([$this,'checkv'])
];
$data = ['min'=>10];
$v = Validator::make($data,$rule);
$rule = [
'min'=>'checkv'
];
Validator::extend('checkv',[$this,'checkv']);
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。