改进 自定义验证规则

This commit is contained in:
thinkphp
2016-03-25 08:11:12 +08:00
parent 00e34b1e85
commit a98f3061e0

View File

@@ -17,7 +17,7 @@ class Validate
protected static $instance = null; protected static $instance = null;
// 自定义的验证类型 // 自定义的验证类型
protected $type = []; protected static $type = [];
// 验证类型别名 // 验证类型别名
protected $alias = [ protected $alias = [
@@ -31,7 +31,7 @@ class Validate
protected $message = []; protected $message = [];
// 验证规则默认提示信息 // 验证规则默认提示信息
protected $typeMsg = [ protected static $typeMsg = [
'require' => ':attribute不能为空', 'require' => ':attribute不能为空',
'number' => ':attribute必须是数字', 'number' => ':attribute必须是数字',
'float' => ':attribute必须是浮点数', 'float' => ':attribute必须是浮点数',
@@ -133,16 +133,31 @@ class Validate
* @access public * @access public
* @param string $type 验证规则类型 * @param string $type 验证规则类型
* @param mixed $callback callback方法(或闭包) * @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)) { if (is_array($type)) {
$this->type = array_merge($this->type, $type); self::$type = array_merge(self::$type, $type);
} else { } 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; 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 * @access public
@@ -331,7 +329,7 @@ class Validate
// 如果不是require 有数据才会行验证 // 如果不是require 有数据才会行验证
if (0 === strpos($info, 'require') || !empty($value)) { 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]); $result = call_user_func_array($callback, [$value, $rule, &$data, $field]);
} else { } else {
@@ -498,8 +496,13 @@ class Validate
$result = is_array($value); $result = is_array($value);
break; break;
default: default:
// 正则验证 if (isset(self::$type[$rule])) {
$result = $this->regex($value, $rule); // 注册的验证规则
$result = call_user_func_array(self::$type[$rule], [$value, &$data]);
} else {
// 正则验证
$result = $this->regex($value, $rule);
}
} }
return $result; return $result;
} }
@@ -859,8 +862,8 @@ class Validate
$msg = $this->message[$attribute . '.' . $type]; $msg = $this->message[$attribute . '.' . $type];
} elseif (isset($this->message[$attribute])) { } elseif (isset($this->message[$attribute])) {
$msg = $this->message[$attribute]; $msg = $this->message[$attribute];
} elseif (isset($this->typeMsg[$type])) { } elseif (isset(self::$typeMsg[$type])) {
$msg = $this->typeMsg[$type]; $msg = self::$typeMsg[$type];
} else { } else {
$msg = $title . '规则错误'; $msg = $title . '规则错误';
} }