自动完成规则支持场景定义

This commit is contained in:
thinkphp
2016-02-18 16:53:35 +08:00
parent 45bf3e46b0
commit 98c94a1317
2 changed files with 51 additions and 52 deletions

View File

@@ -990,43 +990,13 @@ class Model
protected function dataValidate(&$data) protected function dataValidate(&$data)
{ {
if (!empty($this->options['validate'])) { if (!empty($this->options['validate'])) {
if (is_string($this->options['validate'])) { // 获取自动验证规则
// 读取配置文件中的自动验证定义 list($rules, $options, $scene) = $this->getDataRule($this->options['validate'], 'validate');
$validate = Config::get('validate');
if (isset($validate['__pattern__'])) {
// 全局字段规则
$this->rule = $validate['__pattern__'];
}
if (strpos($this->options['validate'], '.')) {
list($name, $group) = explode('.', $this->options['validate']);
} else {
$name = $this->options['validate'];
}
$rules = $validate[$name];
if (isset($validate['__all__'])) {
$rules = array_merge($validate['__all__'], $rules);
}
} else {
$rules = $this->options['validate'];
}
if (isset($rules['__option__'])) {
// 验证参数设置
$options = $rules['__option__'];
unset($rules['__option__']);
} else {
$options = [];
}
if (isset($group) && isset($options['scene'][$group])) {
// 如果设置了验证适用场景
$scene = $options['scene'][$group];
if (is_string($scene)) {
$scene = explode(',', $scene);
}
}
$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 (isset($scene) && !in_array($key, $scene)) { if (!empty($scene) && !in_array($key, $scene)) {
continue; continue;
} }
if (strpos($key, '.')) { if (strpos($key, '.')) {
@@ -1072,26 +1042,15 @@ class Model
protected function dataFill(&$data) protected function dataFill(&$data)
{ {
if (!empty($this->options['auto'])) { if (!empty($this->options['auto'])) {
if (is_string($this->options['auto'])) { // 获取自动完成规则
// 读取配置文件中的自动验证定义 list($rules, $options, $scene) = $this->getDataRule($this->options['auto'], 'auto');
$auto = Config::get('auto');
$rules = $auto[$this->options['auto']];
if (isset($auto['__all__'])) {
$rules = array_merge($auto['__all__'], $rules);
}
} 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['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 (!empty($scene) && !in_array($key, $scene)) {
continue;
}
// 数据自动填充 // 数据自动填充
$this->autoOperation($key, $val, $data, $options); $this->autoOperation($key, $val, $data, $options);
} }
@@ -1099,6 +1058,46 @@ class Model
} }
} }
protected function getDataRule($data, $type)
{
if (is_string($data)) {
// 读取配置文件中的自动验证定义
$config = Config::get($type);
if ('validate' == $type && isset($config['__pattern__'])) {
// 全局字段规则
$this->rule = $config['__pattern__'];
}
if (strpos($data, '.')) {
list($name, $group) = explode('.', $data);
} else {
$name = $data;
}
$rules = $config[$name];
if (isset($config['__all__'])) {
$rules = array_merge($config['__all__'], $rules);
}
} else {
$rules = $data;
}
if (isset($rules['__option__'])) {
// 参数设置
$options = $rules['__option__'];
unset($rules['__option__']);
} else {
$options = [];
}
if (isset($group) && isset($options['scene'][$group])) {
// 如果设置了验证适用场景
$scene = $options['scene'][$group];
if (is_string($scene)) {
$scene = explode(',', $scene);
}
} else {
$scene = [];
}
return [$rules, $options, $scene];
}
/** /**
* 数据自动填充 * 数据自动填充
* @access public * @access public

View File

@@ -22,7 +22,7 @@ Loader::register();
// 加载模式定义文件 // 加载模式定义文件
$mode = require MODE_PATH . APP_MODE . EXT; $mode = require MODE_PATH . APP_MODE . EXT;
// 加载空间别名定义 // 加载模式命名空间定义
if (isset($mode['namespace'])) { if (isset($mode['namespace'])) {
Loader::addNamespace($mode['namespace']); Loader::addNamespace($mode['namespace']);
} }