From fa9eae4a265fae99b3d13db21715f71e49bb3d27 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 20 Mar 2016 11:04:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bmodel=E7=9A=84validate?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Loader.php | 2 ++ library/think/Model.php | 8 +++++--- library/think/Validate.php | 7 +++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/library/think/Loader.php b/library/think/Loader.php index 2232ac57..beb4b4f8 100644 --- a/library/think/Loader.php +++ b/library/think/Loader.php @@ -376,6 +376,8 @@ class Loader $class = str_replace('\\' . $module . '\\', '\\' . COMMON_MODULE . '\\', $class); if (class_exists($class)) { $validate = new $class; + } else { + throw new Exception('class [ ' . $class . ' ] not exists', 10001); } } $_instance[$name . $layer] = $validate; diff --git a/library/think/Model.php b/library/think/Model.php index 54eff7c5..69747601 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -989,12 +989,14 @@ class Model protected function dataValidate(&$data) { if (!empty($this->options['validate'])) { - $info = $this->options['validate']; - $name = is_string($info) ? $info : $this->name; - $validate = Loader::validate($name); + $info = $this->options['validate']; if (is_array($info)) { + $validate = Loader::validate(); $validate->rule($info['rule']); $validate->message($info['msg']); + } else { + $name = is_string($info) ? $info : $this->name; + $validate = Loader::validate($name); } if (!$validate->check($data)) { $this->error = $validate->getError(); diff --git a/library/think/Validate.php b/library/think/Validate.php index 9e1c33f2..a88b18ae 100644 --- a/library/think/Validate.php +++ b/library/think/Validate.php @@ -520,11 +520,14 @@ class Validate * @access public * @param mixed $value 字段值 * @param mixed $rule 验证规则 - * @param mixed $param 参数 * @return bool */ - public function filter($value, $rule, $param = null) + public function filter($value, $rule) { + if (is_string($rule)) { + $rule = explode(',', $rule); + } + list($rule, $param) = $rule; return false !== filter_var($value, is_int($rule) ? $rule : filter_id($rule), $param); }