自动完成增加参数 可以支持 存在就填充 和 有值才填充,如果不设置即为 必须填充

This commit is contained in:
thinkphp
2016-02-16 12:23:22 +08:00
parent e90159ef6d
commit e17c4c090b

View File

@@ -1023,9 +1023,9 @@ class Model
$value = isset($data[$key]) ? $data[$key] : null; $value = isset($data[$key]) ? $data[$key] : null;
} }
if (in_array($key, $options['value_validate']) && '' == $value) { if ((in_array($key, $options['value_validate']) && '' == $value)
continue; || (in_array($key, $options['exists_validate']) && is_null($value))) {
} elseif (in_array($key, $options['exists_validate']) && is_null($value)) { // 不满足自动验证条件
continue; continue;
} }
$result = true; $result = true;
@@ -1066,6 +1066,15 @@ class Model
} else { } else {
$rules = $this->options['auto']; $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) { foreach ($rules as $key => $val) {
// 数据自动填充 // 数据自动填充
$this->autoOperation($key, $val, $data); $this->autoOperation($key, $val, $data);
@@ -1080,9 +1089,10 @@ class Model
* @param string $key 字段名 * @param string $key 字段名
* @param mixed $val 填充规则 * @param mixed $val 填充规则
* @param array $data 数据 * @param array $data 数据
* @param array $options 参数
* @return void * @return void
*/ */
protected function autoOperation($key, $val, &$data) protected function autoOperation($key, $val, &$data, $options = [])
{ {
if (strpos($key, '.')) { if (strpos($key, '.')) {
// 支持二维数组 // 支持二维数组
@@ -1091,6 +1101,11 @@ class Model
} else { } else {
$value = isset($data[$key]) ? $data[$key] : null; $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) { if ($val instanceof \Closure) {
$result = App::invokeFunction($val, [$value, $data]); $result = App::invokeFunction($val, [$value, $data]);
} else { } else {
@@ -1161,7 +1176,7 @@ class Model
// 行为验证 // 行为验证
$result = Hook::exec($rule, '', $data); $result = Hook::exec($rule, '', $data);
break; break;
case 'filter': // 使用filter_var验证 case 'filter': // 使用filter_var验证
$result = filter_var($value, is_int($rule) ? $rule : filter_id($rule), $options); $result = filter_var($value, is_int($rule) ? $rule : filter_id($rule), $options);
break; break;
case 'confirm': case 'confirm':
@@ -1172,8 +1187,8 @@ class Model
$range = is_array($rule) ? $rule : explode(',', $rule); $range = is_array($rule) ? $rule : explode(',', $rule);
$result = 'in' == $type ? in_array($value, $range) : !in_array($value, $range); $result = 'in' == $type ? in_array($value, $range) : !in_array($value, $range);
break; break;
case 'between':// 验证是否在某个范围 case 'between': // 验证是否在某个范围
case 'notbetween': // 验证是否不在某个范围 case 'notbetween': // 验证是否不在某个范围
if (is_string($rule)) { if (is_string($rule)) {
$rule = explode(',', $rule); $rule = explode(',', $rule);
} }