\traits\model\Auto 用于升级兼容原有模型的自动验证和自动完成用法

This commit is contained in:
thinkphp
2016-02-20 09:05:38 +08:00
parent 6508185312
commit eae50abb78
2 changed files with 31 additions and 29 deletions

View File

@@ -1162,6 +1162,8 @@ class Model
$result = call_user_func_array($val, [$value, &$data]); $result = call_user_func_array($val, [$value, &$data]);
} elseif (isset($val[0]) && $val[0] instanceof \Closure) { } elseif (isset($val[0]) && $val[0] instanceof \Closure) {
$result = call_user_func_array($val[0], [$value, &$data]); $result = call_user_func_array($val[0], [$value, &$data]);
} elseif (!is_array($val)) {
$result = $val;
} 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';
@@ -1234,7 +1236,7 @@ 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);

View File

@@ -193,7 +193,7 @@ trait Auto
$auto[3] = 'string'; $auto[3] = 'string';
} }
switch (trim($auto[3])) { switch (trim($auto[3])) {
case 'function': // 使用函数进行填充 字段的值作为参数 case 'function':// 使用函数进行填充 字段的值作为参数
case 'callback': // 使用回调方法 case 'callback': // 使用回调方法
$args = isset($auto[4]) ? (array) $auto[4] : []; $args = isset($auto[4]) ? (array) $auto[4] : [];
if (is_string($auto[0]) && strpos($auto[0], ',')) { if (is_string($auto[0]) && strpos($auto[0], ',')) {
@@ -356,7 +356,7 @@ trait Auto
protected function _validationFieldItem($data, $val) protected function _validationFieldItem($data, $val)
{ {
switch (strtolower(trim($val[4]))) { switch (strtolower(trim($val[4]))) {
case 'function': // 使用函数进行验证 case 'function':// 使用函数进行验证
case 'callback': // 调用方法进行验证 case 'callback': // 调用方法进行验证
$args = isset($val[6]) ? (array) $val[6] : []; $args = isset($val[6]) ? (array) $val[6] : [];
if (is_string($val[0]) && strpos($val[0], ',')) { if (is_string($val[0]) && strpos($val[0], ',')) {
@@ -416,11 +416,11 @@ trait Auto
{ {
$type = strtolower(trim($type)); $type = strtolower(trim($type));
switch ($type) { switch ($type) {
case 'in': // 验证是否在某个指定范围之内 逗号分隔字符串或者数组 case 'in':// 验证是否在某个指定范围之内 逗号分隔字符串或者数组
case 'notin': case 'notin':
$range = is_array($rule) ? $rule : explode(',', $rule); $range = is_array($rule) ? $rule : explode(',', $rule);
return 'in' == $type ? in_array($value, $range) : !in_array($value, $range); return 'in' == $type ? in_array($value, $range) : !in_array($value, $range);
case 'between': // 验证是否在某个范围 case 'between':// 验证是否在某个范围
case 'notbetween': // 验证是否不在某个范围 case 'notbetween': // 验证是否不在某个范围
if (is_array($rule)) { if (is_array($rule)) {
$min = $rule[0]; $min = $rule[0];
@@ -429,7 +429,7 @@ trait Auto
list($min, $max) = explode(',', $rule); list($min, $max) = explode(',', $rule);
} }
return 'between' == $type ? $value >= $min && $value <= $max : $value < $min || $value > $max; return 'between' == $type ? $value >= $min && $value <= $max : $value < $min || $value > $max;
case 'equal': // 验证是否等于某个值 case 'equal':// 验证是否等于某个值
case 'notequal': // 验证是否等于某个值 case 'notequal': // 验证是否等于某个值
return 'equal' == $type ? $value == $rule : $value != $rule; return 'equal' == $type ? $value == $rule : $value != $rule;
case 'length': // 验证长度 case 'length': // 验证长度
@@ -471,7 +471,7 @@ trait Auto
* @param array $auto 自动完成设置 * @param array $auto 自动完成设置
* @return Auto * @return Auto
*/ */
public function auto($auto) public function auto($auto, $rule = null)
{ {
$this->options['auto'] = $auto; $this->options['auto'] = $auto;
return $this; return $this;
@@ -483,7 +483,7 @@ trait Auto
* @param array $validate 自动验证设置 * @param array $validate 自动验证设置
* @return Auto * @return Auto
*/ */
public function validate($validate) public function validate($validate, $rule = null)
{ {
$this->options['validate'] = $validate; $this->options['validate'] = $validate;
return $this; return $this;