Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复自定义msg数组形式的无效果 #56

Merged
merged 1 commit into from
Jul 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
);
}
}