Skip to content

Commit

Permalink
允许设置错误规则映射
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Jun 11, 2021
1 parent a1a107e commit 3657c82
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Error
*/
private $validate;

private $defaultErrorMsg = [
private $ruleMsgMapping = [
'activeUrl' => ':fieldName必须是可访问的网址',
'alpha' => ':fieldName只能是字母',
'alphaNum' => ':fieldName只能是字母和数字',
Expand Down Expand Up @@ -162,7 +162,13 @@ public function getErrorRuleMsg(): string
return $this->errorRuleMsg;
}

return $this->parserDefaultErrorMsg();
return $this->parserRuleMsg();
}

function setRuleMsgMapping(array $mapping):Error
{
$this->ruleMsgMapping = $mapping;
return $this;
}

/**
Expand Down Expand Up @@ -205,13 +211,13 @@ public function setValidate(?Validate $validate): void
* 组装默认错误消息
* @return mixed|string
*/
private function parserDefaultErrorMsg()
private function parserRuleMsg()
{
$fieldName = empty($this->fieldAlias) ? $this->field : $this->fieldAlias;
if (!isset($this->defaultErrorMsg[$this->errorRule])) {
if (!isset($this->ruleMsgMapping[$this->errorRule])) {
return "{$fieldName}参数错误";
}
$defaultErrorTpl = $this->defaultErrorMsg[$this->errorRule];
$defaultErrorTpl = $this->ruleMsgMapping[$this->errorRule];
$errorMsg = str_replace(':fieldName', $fieldName, $defaultErrorTpl);

if (in_array($this->errorRule, ['equalWithColumn', 'differentWithColumn', 'lessThanWithColumn', 'greaterThanWithColumn'])) {
Expand Down

0 comments on commit 3657c82

Please sign in to comment.