mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
改进validate类的unique验证规则的条件
This commit is contained in:
@@ -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,7 +313,6 @@ 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) {
|
||||||
@@ -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, '');
|
||||||
|
|||||||
Reference in New Issue
Block a user