改进validate类的unique验证规则的条件

This commit is contained in:
thinkphp
2016-03-21 16:42:34 +08:00
parent 6643b38626
commit ebe350e35f

View File

@@ -301,7 +301,7 @@ class Validate
* @param array $data 数据 * @param array $data 数据
* @param string $title 字段描述 * @param string $title 字段描述
* @param array $msg 提示信息 * @param array $msg 提示信息
* @return string|true * @return mixed
*/ */
protected function checkItem($field, $value, $rules, &$data, $title = '', $msg = []) protected function checkItem($field, $value, $rules, &$data, $title = '', $msg = [])
{ {
@@ -313,8 +313,7 @@ class Validate
if (is_string($rules)) { if (is_string($rules)) {
$rules = explode('|', $rules); $rules = explode('|', $rules);
} }
$error = []; $i = 0;
$i = 0;
foreach ($rules as $key => $rule) { foreach ($rules as $key => $rule) {
if ($rule instanceof \Closure) { if ($rule instanceof \Closure) {
$result = call_user_func_array($rule, [$value, &$data]); $result = call_user_func_array($rule, [$value, &$data]);
@@ -344,22 +343,20 @@ class Validate
if (false === $result) { if (false === $result) {
// 验证失败 返回错误信息 // 验证失败 返回错误信息
$message = isset($msg[$i]) ? $msg[$i] : null; if (isset($msg[$i])) {
$error[] = $this->getRuleMsg($field, $title, $info, $rule, $message); $message = $msg[$i];
} elseif (is_string($result)) { } else {
$error[] = $result; $message = $this->getRuleMsg($field, $title, $info, $rule);
} elseif (is_array($result)) { }
// 自定义错误信息数组 return $message;
} elseif (true !== $result) {
// 返回自定义错误信息
return $result; return $result;
} }
$i++; $i++;
} }
if (!empty($error)) {
$result = implode(',', $error);
}
} }
// 验证失败返回错误信息 return true !== $result ? $result : true;
return $result;
} }
/** /**
@@ -544,7 +541,19 @@ class Validate
} elseif (isset($data[$key])) { } elseif (isset($data[$key])) {
$except = $data[$key]; $except = $data[$key];
} }
$map[$field] = $value;
if (strpos($field, '|')) {
// 支持多个字段验证
$fields = explode('|', $field);
foreach ($fields as $field) {
$map[$field] = $data[$field];
}
} elseif (strpos($field, '=')) {
parse_str($field, $map);
} else {
$map[$field] = $data[$field];
}
if (isset($except)) { if (isset($except)) {
$map[$key] = ['neq', $except]; $map[$key] = ['neq', $except];
} }
@@ -846,14 +855,11 @@ class Validate
* @param string $title 字段描述名 * @param string $title 字段描述名
* @param string $type 验证规则名称 * @param string $type 验证规则名称
* @param mixed $rule 验证规则数据 * @param mixed $rule 验证规则数据
* @param string $message 自定义提示信息
* @return string * @return string
*/ */
protected function getRuleMsg($attribute, $title, $type, $rule, $message) protected function getRuleMsg($attribute, $title, $type, $rule)
{ {
if (!is_null($message)) { if (isset($this->message[$attribute . '.' . $type])) {
$msg = $message;
} elseif (isset($this->message[$attribute . '.' . $type])) {
$msg = $this->message[$attribute . '.' . $type]; $msg = $this->message[$attribute . '.' . $type];
} elseif (isset($this->message[$attribute])) { } elseif (isset($this->message[$attribute])) {
$msg = $this->message[$attribute]; $msg = $this->message[$attribute];
@@ -863,8 +869,7 @@ class Validate
$msg = $title . '规则错误'; $msg = $title . '规则错误';
} }
// TODO 多语言支持 // TODO 多语言支持
if (is_string($msg) && false !== strpos($msg, ':')) {
if (false !== strpos($msg, ':')) {
// 变量替换 // 变量替换
if (strpos($rule, ',')) { if (strpos($rule, ',')) {
$array = array_pad(explode(',', $rule), 3, ''); $array = array_pad(explode(',', $rule), 3, '');