From a98f3061e0dbed32ab88f673e70c149da4729b64 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 25 Mar 2016 08:11:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E9=AA=8C=E8=AF=81=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Validate.php | 61 ++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/library/think/Validate.php b/library/think/Validate.php index 601e301a..ed34bb95 100644 --- a/library/think/Validate.php +++ b/library/think/Validate.php @@ -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 . '规则错误'; }