Skip to content

Commit fd5e556

Browse files
committed
fix: #10 Error message is not right
1 parent 3b09b87 commit fd5e556

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/Traits/ErrorMessageTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,11 @@ protected function findMessage(string $field, string $rawName)
338338
$fullKey = $field . '.' . $rawName;
339339
$realName = Validators::realName($rawName);
340340

341+
// get from default
342+
if (!$this->_messages) {
343+
return GlobalMessage::get($realName);
344+
}
345+
341346
if (isset($this->_messages[$fullKey])) {
342347
$message = $this->_messages[$fullKey];
343348
// eg 'required' => 'some message ...'

src/Validator/GlobalMessage.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ final class GlobalMessage
1414
*/
1515
private static $messages = [
1616
// 'int' 'integer'
17-
'integer' => '{attr} must be an integer!',
17+
'integer' => [
18+
'{attr} must be an integer!',
19+
'{attr} must be an integer and minimum value is {min}',
20+
'{attr} must be an integer and value range {min} ~ {max}',
21+
],
1822
// 'num'
1923
'number' => [
2024
'{attr} must be an integer greater than 0!',

test/Traits/ErrorMessageTraitTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Inhere\ValidateTest\Traits;
44

55
use Inhere\Validate\FieldValidation;
6+
use Inhere\Validate\Validation;
67
use PHPUnit\Framework\TestCase;
78

89
/**
@@ -103,4 +104,19 @@ public function testFieldTranslate()
103104
$v->clearTranslates();
104105
$this->assertEmpty($v->getTranslates());
105106
}
107+
108+
/**
109+
* for https://github.com/inhere/php-validate/issues/10
110+
*/
111+
public function testForIssues10()
112+
{
113+
$v = Validation::check([
114+
'page' => 0
115+
], [
116+
['page', 'integer', 'min' => 1]
117+
]);
118+
119+
$this->assertTrue($v->isFail());
120+
$this->assertSame('page must be an integer and minimum value is 1', $v->firstError());
121+
}
106122
}

0 commit comments

Comments
 (0)