From 7c1d29595bf0f63076411009104d9d835068408f Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 7 Mar 2016 18:02:02 +0800 Subject: [PATCH] =?UTF-8?q?Validate=E7=B1=BB=E6=94=AF=E6=8C=81=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E6=A8=A1=E5=9E=8B=E7=B1=BB=E7=9A=84=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=AE=8C=E6=88=90=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 14 ++++++++++++++ library/think/Validate.php | 15 +++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index a178d481..d92381b2 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1817,4 +1817,18 @@ class Model } return $sql; } + + /** + * 获取属性值 + * @access protected + * @param string $property 属性名 + * @return mixed + */ + public function getProperty($property) + { + if (property_exists($this, $property)) { + return $this->$property; + } + return null; + } } diff --git a/library/think/Validate.php b/library/think/Validate.php index 301aa4a9..2c7dc0b5 100644 --- a/library/think/Validate.php +++ b/library/think/Validate.php @@ -49,7 +49,7 @@ class Validate * 数据自动验证 * @access protected * @param array $data 数据 - * @param array $rules 验证规则 + * @param mixed $rules 验证规则 * @param string $config 规则配置名 验证规则不是数组的话读取配置参数 * @return void */ @@ -213,7 +213,7 @@ class Validate // 行为验证 $result = self::behavior($rule, $data); break; - case 'filter': // 使用filter_var验证 + case 'filter': // 使用filter_var验证 $result = self::filter($value, $rule, $options); break; case 'confirm': @@ -225,10 +225,10 @@ class Validate case 'notin': $result = self::notin($value, $rule); break; - case 'between': // 验证是否在某个范围 + case 'between': // 验证是否在某个范围 $result = self::between($value, $rule); break; - case 'notbetween': // 验证是否不在某个范围 + case 'notbetween': // 验证是否不在某个范围 $result = self::notbetween($value, $rule); break; case 'expire': @@ -263,7 +263,7 @@ class Validate */ public static function confirm($value, $rule, $data) { - return $value == $data[$rule]; + return self::getDataValue($data, $rule) == $value; } /** @@ -517,7 +517,10 @@ class Validate */ protected static function getDataRule($rules, $config) { - if (!is_array($rules)) { + if ($rules instanceof \think\Model) { + // 读取模型设置的规则 + $rules = $rules->getProperty($config); + } elseif (is_string($rules)) { // 读取配置文件中的数据类型定义 $config = Config::get($config); if (isset($config['__pattern__'])) {