mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
Validate类增加checkRule方法用于静态验证多个规则
This commit is contained in:
@@ -340,6 +340,43 @@ class Validate
|
||||
return !empty($this->error) ? false : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据验证规则验证数据
|
||||
* @access protected
|
||||
* @param mixed $value 字段值
|
||||
* @param mixed $rules 验证规则
|
||||
* @return bool
|
||||
*/
|
||||
protected function checkRule($value, $rules)
|
||||
{
|
||||
if ($rules instanceof \Closure) {
|
||||
return call_user_func_array($rules, [$value]);
|
||||
} elseif ($rules instanceof ValidateRule) {
|
||||
$rules = $rules->getRule();
|
||||
} elseif (is_string($rules)) {
|
||||
$rules = explode('|', $rules);
|
||||
}
|
||||
|
||||
foreach ($rules as $key => $rule) {
|
||||
if ($rule instanceof \Closure) {
|
||||
$result = call_user_func_array($rule, [$value]);
|
||||
} else {
|
||||
// 判断验证类型
|
||||
list($type, $rule) = $this->getValidateType($key, $rule);
|
||||
|
||||
$callback = isset(self::$type[$type]) ? self::$type[$type] : [$this, $type];
|
||||
|
||||
$result = call_user_func_array($callback, [$value, $rule]);
|
||||
}
|
||||
|
||||
if (true !== $result) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证单个字段规则
|
||||
* @access protected
|
||||
@@ -364,25 +401,7 @@ class Validate
|
||||
$info = is_numeric($key) ? '' : $key;
|
||||
} else {
|
||||
// 判断验证类型
|
||||
if (is_numeric($key)) {
|
||||
if (strpos($rule, ':')) {
|
||||
list($type, $rule) = explode(':', $rule, 2);
|
||||
if (isset($this->alias[$type])) {
|
||||
// 判断别名
|
||||
$type = $this->alias[$type];
|
||||
}
|
||||
$info = $type;
|
||||
} elseif (method_exists($this, $rule)) {
|
||||
$type = $rule;
|
||||
$info = $rule;
|
||||
$rule = '';
|
||||
} else {
|
||||
$type = 'is';
|
||||
$info = $rule;
|
||||
}
|
||||
} else {
|
||||
$info = $type = $key;
|
||||
}
|
||||
list($type, $rule, $info) = $this->getValidateType($key, $rule);
|
||||
|
||||
// 如果不是require 有数据才会行验证
|
||||
if (0 === strpos($info, 'require') || (!is_null($value) && '' !== $value)) {
|
||||
@@ -418,6 +437,39 @@ class Validate
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前验证类型及规则
|
||||
* @access public
|
||||
* @param mixed $key
|
||||
* @param mixed $rule
|
||||
* @return array
|
||||
*/
|
||||
protected function getValidateType($key, $rule)
|
||||
{
|
||||
// 判断验证类型
|
||||
if (!is_numeric($key)) {
|
||||
return [$key, $rule, $key];
|
||||
}
|
||||
|
||||
if (strpos($rule, ':')) {
|
||||
list($type, $rule) = explode(':', $rule, 2);
|
||||
if (isset($this->alias[$type])) {
|
||||
// 判断别名
|
||||
$type = $this->alias[$type];
|
||||
}
|
||||
$info = $type;
|
||||
} elseif (method_exists($this, $rule)) {
|
||||
$type = $rule;
|
||||
$info = $rule;
|
||||
$rule = '';
|
||||
} else {
|
||||
$type = 'is';
|
||||
$info = $rule;
|
||||
}
|
||||
|
||||
return [$type, $rule, $info];
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证是否和某个字段的值一致
|
||||
* @access protected
|
||||
|
||||
Reference in New Issue
Block a user