From b63bda78d76a13c842058052a529d1e1b47432b8 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 9 Feb 2016 20:22:53 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=AA=8C=E8=AF=81=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20=E6=89=B9=E9=87=8F=E9=AA=8C=E8=AF=81=E5=92=8C=20?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E5=B0=B1=E9=AA=8C=E8=AF=81=20=E6=9C=89?= =?UTF-8?q?=E5=80=BC=E5=B0=B1=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 | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 60f1d1e9..2bc38d6c 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -948,8 +948,23 @@ class Model } else { $rules = $this->options['validate']; } + if (isset($rules['__option__'])) { + // 验证参数设置 + $options = $rules['__option__']; + unset($rules['__option__']); + } else { + $options = []; + } + $options['value_validate'] = isset($options['value_validate']) ? $options['value_validate'] : []; + $options['exists_validate'] = isset($options['exists_validate']) ? $options['exists_validate'] : []; foreach ($rules as $key => $val) { $value = isset($data[$key]) ? $data[$key] : null; + if (in_array($key, $options['value_validate']) && '' == $value) { + continue; + } elseif (in_array($key, $options['exists_validate']) && !isset($data[$key])) { + continue; + } + $result = true; if ($val instanceof \Closure) { // 匿名函数验证 支持传入当前字段和所有字段两个数据 $result = App::invokeFunction($val, [$value, $data]); @@ -959,10 +974,19 @@ class Model } if (true !== $result) { // 没有返回true 则表示验证失败 - $this->error = $result; - return false; + if (!empty($options['patch'])) { + // 批量验证 + $this->error[$key] = $result; + } else { + $this->error = $result; + return false; + } } } + // 批量验证的时候最后返回错误 + if (!empty($this->error)) { + return false; + } } // 数据自动填充