mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
改进 自定义验证规则
This commit is contained in:
@@ -17,7 +17,7 @@ class Validate
|
||||
protected static $instance = null;
|
||||
|
||||
// 自定义的验证类型
|
||||
protected $type = [];
|
||||
protected static $type = [];
|
||||
|
||||
// 验证类型别名
|
||||
protected $alias = [
|
||||
@@ -31,7 +31,7 @@ class Validate
|
||||
protected $message = [];
|
||||
|
||||
// 验证规则默认提示信息
|
||||
protected $typeMsg = [
|
||||
protected static $typeMsg = [
|
||||
'require' => ':attribute不能为空',
|
||||
'number' => ':attribute必须是数字',
|
||||
'float' => ':attribute必须是浮点数',
|
||||
@@ -133,16 +133,31 @@ class Validate
|
||||
* @access public
|
||||
* @param string $type 验证规则类型
|
||||
* @param mixed $callback callback方法(或闭包)
|
||||
* @return Validate
|
||||
* @return void
|
||||
*/
|
||||
public function extend($type, $callback = null)
|
||||
public static function extend($type, $callback = null)
|
||||
{
|
||||
if (is_array($type)) {
|
||||
$this->type = array_merge($this->type, $type);
|
||||
self::$type = array_merge(self::$type, $type);
|
||||
} else {
|
||||
$this->type[$type] = $callback;
|
||||
self::$type[$type] = $callback;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取验证规则的默认提示信息
|
||||
* @access protected
|
||||
* @param string|array $type 验证规则类型名称或者数组
|
||||
* @param string $msg 验证提示信息
|
||||
* @return void
|
||||
*/
|
||||
public static function setTypeMsg($type, $msg = null)
|
||||
{
|
||||
if (is_array($type)) {
|
||||
self::$typeMsg = array_merge(self::$typeMsg, $type);
|
||||
} else {
|
||||
self::$typeMsg[$type] = $msg;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -162,23 +177,6 @@ class Validate
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取验证规则的默认提示信息
|
||||
* @access protected
|
||||
* @param string|array $type 验证规则类型名称或者数组
|
||||
* @param string $msg 验证提示信息
|
||||
* @return Validate
|
||||
*/
|
||||
public function setTypeMsg($type, $msg = null)
|
||||
{
|
||||
if (is_array($type)) {
|
||||
$this->typeMsg = array_merge($this->typeMsg, $type);
|
||||
} else {
|
||||
$this->typeMsg[$type] = $msg;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置验证场景
|
||||
* @access public
|
||||
@@ -331,7 +329,7 @@ class Validate
|
||||
// 如果不是require 有数据才会行验证
|
||||
if (0 === strpos($info, 'require') || !empty($value)) {
|
||||
// 验证类型
|
||||
$callback = isset($this->type[$type]) ? $this->type[$type] : [$this, $type];
|
||||
$callback = isset(self::$type[$type]) ? self::$type[$type] : [$this, $type];
|
||||
// 验证数据
|
||||
$result = call_user_func_array($callback, [$value, $rule, &$data, $field]);
|
||||
} else {
|
||||
@@ -498,8 +496,13 @@ class Validate
|
||||
$result = is_array($value);
|
||||
break;
|
||||
default:
|
||||
// 正则验证
|
||||
$result = $this->regex($value, $rule);
|
||||
if (isset(self::$type[$rule])) {
|
||||
// 注册的验证规则
|
||||
$result = call_user_func_array(self::$type[$rule], [$value, &$data]);
|
||||
} else {
|
||||
// 正则验证
|
||||
$result = $this->regex($value, $rule);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
@@ -859,8 +862,8 @@ class Validate
|
||||
$msg = $this->message[$attribute . '.' . $type];
|
||||
} elseif (isset($this->message[$attribute])) {
|
||||
$msg = $this->message[$attribute];
|
||||
} elseif (isset($this->typeMsg[$type])) {
|
||||
$msg = $this->typeMsg[$type];
|
||||
} elseif (isset(self::$typeMsg[$type])) {
|
||||
$msg = self::$typeMsg[$type];
|
||||
} else {
|
||||
$msg = $title . '规则错误';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user