From e17c4c090b686362096a0f18a2c19749f1e060b3 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 16 Feb 2016 12:23:22 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=AE=8C=E6=88=90=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=8F=82=E6=95=B0=20=E5=8F=AF=E4=BB=A5=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20=E5=AD=98=E5=9C=A8=E5=B0=B1=E5=A1=AB=E5=85=85=20?= =?UTF-8?q?=E5=92=8C=20=E6=9C=89=E5=80=BC=E6=89=8D=E5=A1=AB=E5=85=85?= =?UTF-8?q?=EF=BC=8C=E5=A6=82=E6=9E=9C=E4=B8=8D=E8=AE=BE=E7=BD=AE=E5=8D=B3?= =?UTF-8?q?=E4=B8=BA=20=E5=BF=85=E9=A1=BB=E5=A1=AB=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index ed0d8666..b395554d 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1023,9 +1023,9 @@ class Model $value = isset($data[$key]) ? $data[$key] : null; } - if (in_array($key, $options['value_validate']) && '' == $value) { - continue; - } elseif (in_array($key, $options['exists_validate']) && is_null($value)) { + if ((in_array($key, $options['value_validate']) && '' == $value) + || (in_array($key, $options['exists_validate']) && is_null($value))) { + // 不满足自动验证条件 continue; } $result = true; @@ -1066,6 +1066,15 @@ class Model } else { $rules = $this->options['auto']; } + if (isset($rules['__option__'])) { + // 验证参数设置 + $options = $rules['__option__']; + unset($rules['__option__']); + } else { + $options = []; + } + $options['value_fill'] = isset($options['value_fill']) ? $options['value_fill'] : []; + $options['exists_fill'] = isset($options['exists_fill']) ? $options['exists_fill'] : []; foreach ($rules as $key => $val) { // 数据自动填充 $this->autoOperation($key, $val, $data); @@ -1080,9 +1089,10 @@ class Model * @param string $key 字段名 * @param mixed $val 填充规则 * @param array $data 数据 + * @param array $options 参数 * @return void */ - protected function autoOperation($key, $val, &$data) + protected function autoOperation($key, $val, &$data, $options = []) { if (strpos($key, '.')) { // 支持二维数组 @@ -1091,6 +1101,11 @@ class Model } else { $value = isset($data[$key]) ? $data[$key] : null; } + if ((in_array($key, $options['value_fill']) && '' == $value) + || (in_array($key, $options['exists_fill']) && is_null($value))) { + // 不满足自动填充条件 + return; + } if ($val instanceof \Closure) { $result = App::invokeFunction($val, [$value, $data]); } else { @@ -1161,7 +1176,7 @@ class Model // 行为验证 $result = Hook::exec($rule, '', $data); break; - case 'filter': // 使用filter_var验证 + case 'filter': // 使用filter_var验证 $result = filter_var($value, is_int($rule) ? $rule : filter_id($rule), $options); break; case 'confirm': @@ -1172,8 +1187,8 @@ class Model $range = is_array($rule) ? $rule : explode(',', $rule); $result = 'in' == $type ? in_array($value, $range) : !in_array($value, $range); break; - case 'between':// 验证是否在某个范围 - case 'notbetween': // 验证是否不在某个范围 + case 'between': // 验证是否在某个范围 + case 'notbetween': // 验证是否不在某个范围 if (is_string($rule)) { $rule = explode(',', $rule); }