public static function checkData(array $rule, array $data = NULL, &$return_data = NULL, $db = null) { if (is_null($data)) { $data = MpInput::post(); } $return_data = $data; /** * 验证前默认值规则处理 */ foreach ($rule as $col => $val) { //提取出默认值 foreach ($val as $_rule => $msg) { if (stripos($_rule, 'default[') === 0) { //删除默认值规则 unset($rule[$col][$_rule]); $matches = self::getCheckRuleInfo($_rule); $_r = $matches[1]; $args = $matches[2]; $return_data[$col] = isset($args[0]) ? $args[0] : ''; } } } /** * 验证前默认值规则处理,没有默认值就补空 * 并标记最后要清理的key */ $unset_keys = array(); foreach ($rule as $col => $val) { if (!isset($return_data[$col])) { $return_data[$col] = ''; $unset_keys[] = $col; } } /** * 验证前set处理 */ self::checkSetData('set', $rule, $return_data); /** * 验证规则 */ foreach ($rule as $col => $val) { foreach ($val as $_rule => $msg) { if (!empty($_rule)) { /** * 可以为空规则检测 */ if (empty($return_data[$col]) && isset($val['optional'])) { //当前字段,验证通过 break; } else { $matches = self::getCheckRuleInfo($_rule); $_r = $matches[1]; $args = $matches[2]; if ($_r == 'set' || $_r == 'set_post' || $_r == 'optional') { continue; } if (!self::checkRule($_rule, $return_data[$col], $return_data, $db)) { /** * 清理没有传递的key */ foreach ($unset_keys as $key) { unset($return_data[$key]); } return $msg; } } } } } /** * 验证后set_post处理 */ self::checkSetData('set_post', $rule, $return_data); /** * 清理没有传递的key */ foreach ($unset_keys as $key) { unset($return_data[$key]); } return NULL; }
public static function checkData(array $rule, array $data = NULL, &$return_data = NULL, $db = null) { if (is_null($data)) { $data = MpInput::post(); } $return_data = $data; foreach ($rule as $col => $val) { foreach ($val as $_rule => $msg) { if (stripos($_rule, 'default[') === 0) { unset($rule[$col][$_rule]); $matches = self::getCheckRuleInfo($_rule); $_r = $matches[1]; $args = $matches[2]; $return_data[$col] = isset($args[0]) ? $args[0] : ''; } } } $unset_keys = array(); foreach ($rule as $col => $val) { if (!isset($return_data[$col])) { $return_data[$col] = ''; $unset_keys[] = $col; } } self::checkSetData('set', $rule, $return_data); foreach ($rule as $col => $val) { foreach ($val as $_rule => $msg) { if (!empty($_rule)) { if (empty($return_data[$col]) && isset($val['optional'])) { break; } else { $matches = self::getCheckRuleInfo($_rule); $_r = $matches[1]; $args = $matches[2]; if ($_r == 'set' || $_r == 'set_post' || $_r == 'optional') { continue; } if (!self::checkRule($_rule, $return_data[$col], $return_data, $db)) { foreach ($unset_keys as $key) { unset($return_data[$key]); } return $msg; } } } } } self::checkSetData('set_post', $rule, $return_data); foreach ($unset_keys as $key) { unset($return_data[$key]); } return NULL; }