mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
修正model类
This commit is contained in:
@@ -996,7 +996,7 @@ class Model
|
|||||||
$options['value_validate'] = isset($options['value_validate']) ? $options['value_validate'] : [];
|
$options['value_validate'] = isset($options['value_validate']) ? $options['value_validate'] : [];
|
||||||
$options['exists_validate'] = isset($options['exists_validate']) ? $options['exists_validate'] : [];
|
$options['exists_validate'] = isset($options['exists_validate']) ? $options['exists_validate'] : [];
|
||||||
foreach ($rules as $key => $val) {
|
foreach ($rules as $key => $val) {
|
||||||
if (is_numeric($key)) {
|
if (is_numeric($key) && is_array($val)) {
|
||||||
$key = array_shift($val);
|
$key = array_shift($val);
|
||||||
}
|
}
|
||||||
if (!empty($scene) && !in_array($key, $scene)) {
|
if (!empty($scene) && !in_array($key, $scene)) {
|
||||||
@@ -1051,7 +1051,7 @@ class Model
|
|||||||
$options['value_fill'] = isset($options['value_fill']) ? $options['value_fill'] : [];
|
$options['value_fill'] = isset($options['value_fill']) ? $options['value_fill'] : [];
|
||||||
$options['exists_fill'] = isset($options['exists_fill']) ? $options['exists_fill'] : [];
|
$options['exists_fill'] = isset($options['exists_fill']) ? $options['exists_fill'] : [];
|
||||||
foreach ($rules as $key => $val) {
|
foreach ($rules as $key => $val) {
|
||||||
if (is_numeric($key)) {
|
if (is_numeric($key) && is_array($val)) {
|
||||||
$key = array_shift($val);
|
$key = array_shift($val);
|
||||||
}
|
}
|
||||||
if (!empty($scene) && !in_array($key, $scene)) {
|
if (!empty($scene) && !in_array($key, $scene)) {
|
||||||
@@ -1129,13 +1129,12 @@ class Model
|
|||||||
}
|
}
|
||||||
if ($val instanceof \Closure) {
|
if ($val instanceof \Closure) {
|
||||||
$result = call_user_func_array($val, [$value, &$data]);
|
$result = call_user_func_array($val, [$value, &$data]);
|
||||||
|
} elseif (isset($val[0]) && $val[0] instanceof \Closure) {
|
||||||
|
$result = call_user_func_array($rule, [$value, &$data]);
|
||||||
} else {
|
} else {
|
||||||
$rule = isset($val[0]) ? $val[0] : $val;
|
$rule = isset($val[0]) ? $val[0] : $val;
|
||||||
$type = isset($val[1]) ? $val[1] : 'value';
|
$type = isset($val[1]) ? $val[1] : 'value';
|
||||||
$params = isset($val[2]) ? $val[2] : [];
|
$params = isset($val[2]) ? $val[2] : [];
|
||||||
if ($rule instanceof \Closure) {
|
|
||||||
$type = 'callback';
|
|
||||||
}
|
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
case 'behavior':
|
case 'behavior':
|
||||||
Hook::exec($rule, '', $data);
|
Hook::exec($rule, '', $data);
|
||||||
@@ -1181,43 +1180,45 @@ class Model
|
|||||||
$type = isset($val[2]) ? $val[2] : 'regex';
|
$type = isset($val[2]) ? $val[2] : 'regex';
|
||||||
$options = isset($val[3]) ? $val[3] : [];
|
$options = isset($val[3]) ? $val[3] : [];
|
||||||
if ($rule instanceof \Closure) {
|
if ($rule instanceof \Closure) {
|
||||||
$type = 'callback';
|
// 匿名函数验证 支持传入当前字段和所有字段两个数据
|
||||||
}
|
$result = call_user_func_array($rule, [$value, &$data]);
|
||||||
switch ($type) {
|
} else {
|
||||||
case 'callback':
|
switch ($type) {
|
||||||
array_unshift($options, $value);
|
case 'callback':
|
||||||
$result = call_user_func_array($rule, $options);
|
array_unshift($options, $value);
|
||||||
break;
|
$result = call_user_func_array($rule, $options);
|
||||||
case 'behavior':
|
break;
|
||||||
// 行为验证
|
case 'behavior':
|
||||||
$result = Hook::exec($rule, '', $data);
|
// 行为验证
|
||||||
break;
|
$result = Hook::exec($rule, '', $data);
|
||||||
case 'filter': // 使用filter_var验证
|
break;
|
||||||
$result = filter_var($value, is_int($rule) ? $rule : filter_id($rule), $options);
|
case 'filter': // 使用filter_var验证
|
||||||
break;
|
$result = filter_var($value, is_int($rule) ? $rule : filter_id($rule), $options);
|
||||||
case 'confirm':
|
break;
|
||||||
$result = $value == $data[$rule];
|
case 'confirm':
|
||||||
break;
|
$result = $value == $data[$rule];
|
||||||
case 'in':
|
break;
|
||||||
case 'notin':
|
case 'in':
|
||||||
$range = is_array($rule) ? $rule : explode(',', $rule);
|
case 'notin':
|
||||||
$result = 'in' == $type ? in_array($value, $range) : !in_array($value, $range);
|
$range = is_array($rule) ? $rule : explode(',', $rule);
|
||||||
break;
|
$result = 'in' == $type ? in_array($value, $range) : !in_array($value, $range);
|
||||||
case 'between': // 验证是否在某个范围
|
break;
|
||||||
case 'notbetween': // 验证是否不在某个范围
|
case 'between': // 验证是否在某个范围
|
||||||
if (is_string($rule)) {
|
case 'notbetween': // 验证是否不在某个范围
|
||||||
$rule = explode(',', $rule);
|
if (is_string($rule)) {
|
||||||
}
|
$rule = explode(',', $rule);
|
||||||
list($min, $max) = $rule;
|
}
|
||||||
$result = 'between' == $type ? $value >= $min && $value <= $max : $value < $min || $value > $max;
|
list($min, $max) = $rule;
|
||||||
break;
|
$result = 'between' == $type ? $value >= $min && $value <= $max : $value < $min || $value > $max;
|
||||||
case 'regex':
|
break;
|
||||||
default:
|
case 'regex':
|
||||||
if (isset($this->rule[$rule])) {
|
default:
|
||||||
$rule = $this->rule[$rule];
|
if (isset($this->rule[$rule])) {
|
||||||
}
|
$rule = $this->rule[$rule];
|
||||||
$result = 1 === preg_match('/^' . $rule . '$/', (string) $value);
|
}
|
||||||
break;
|
$result = 1 === preg_match('/^' . $rule . '$/', (string) $value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// 验证失败返回错误信息
|
// 验证失败返回错误信息
|
||||||
return (is_array($result) || true === $result) ? $result : $msg;
|
return (is_array($result) || true === $result) ? $result : $msg;
|
||||||
|
|||||||
Reference in New Issue
Block a user