Skip to content

Commit e900fac

Browse files
committed
fix: error message render is not right
1 parent 24c6757 commit e900fac

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/Traits/ErrorMessageTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,11 @@ public function getMessage($validator, string $field, array $args = [], $message
309309

310310
/** @see GlobalMessage::$messages['size'] */
311311
if (\is_array($message)) {
312-
$msgKey = \count($params) - 1;
312+
$msgKey = \count($args);
313313
$message = $message[$msgKey] ?? $message[0];
314314
}
315315

316-
if (\is_string($message) && false === \strpos($message, '{')) {
316+
if (false === \strpos($message, '{')) {
317317
return $message;
318318
}
319319

test/FieldValidationTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class FieldValidationTest extends TestCase
2626
],
2727
];
2828

29-
public function testValidate()
29+
public function testValidateField()
3030
{
3131
$rules = [
3232
['freeTime', 'required'],
@@ -58,6 +58,24 @@ public function testValidate()
5858
$errors = $v->getErrors();
5959
$this->assertNotEmpty($errors);
6060
$this->assertCount(1, $errors);
61+
62+
$v = FieldValidation::check([
63+
'title' => 'hello',
64+
], [
65+
['title', 'required|string:2,8']
66+
]);
67+
$this->assertTrue($v->isOk());
68+
69+
$v = FieldValidation::check([
70+
'title' => 'hello',
71+
], [
72+
['title', 'required|string:1,3']
73+
]);
74+
$this->assertTrue($v->isFail());
75+
$this->assertSame(
76+
'title must be a string and length range must be 1 ~ 3',
77+
$v->firstError()
78+
);
6179
}
6280

6381
public function testScenarios()

0 commit comments

Comments
 (0)