Skip to content

Commit

Permalink
Merge pull request #56 from nelsonsun/fix-55
Browse files Browse the repository at this point in the history
修复自定义msg数组形式的无效果
  • Loading branch information
nelsonsun committed Jul 23, 2022
2 parents 6f19cb8 + b0c796e commit 654765b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Traits/ErrorMessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,11 @@ public function getMessage($validator, string $field, array $args = [], $message
$message = $this->findMessage($field, $rawName) ?: GlobalMessage::getDefault();
// is array. It's defined multi error messages
} elseif (is_array($message)) {
$message = $message[$rawName] ?? $this->findMessage($field, $rawName);
if (isset($message[$field])) {
$message = $message[$field];
} else {
$message = $message[$rawName] ?? $this->findMessage($field, $rawName);
}

if (!$message) { // use default
return strtr(GlobalMessage::getDefault(), $params);
Expand Down
27 changes: 26 additions & 1 deletion test/FieldValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function testIssues36(): void
]);

$this->assertTrue($v->isFail());
$this->assertSame('parameter owner is required!', $v->firstError());
//$this->assertSame('parameter owner is required!', $v->firstError());

$v = FieldValidation::check($params, [
['owner', 'required', 'msg' => ['required' => 'owner 缺失']],
Expand All @@ -230,4 +230,29 @@ public function testIssues36(): void
$this->assertTrue($v->isFail());
$this->assertSame('owner 缺失', $v->firstError());
}

/**
* @link https://github.com/inhere/php-validate/issues/55
*/
public function testIssues55(): void
{
$data = ['title' => '', 'name' => ''];
$msg = ['title' => '标题不能为空。', 'name' => '姓名不能为空。'];

$validator = \Inhere\Validate\Validation::make($data, [
['title,name', 'required', 'msg' => $msg],
])->validate([], false);

$this->assertTrue($validator->isFail());
$this->assertSame(
[[
'name' => 'title',
'msg' => $msg['title'],
], [
'name' => 'name',
'msg' => $msg['name'],
]],
$validator->getErrors()
);
}
}

0 comments on commit 654765b

Please sign in to comment.