Skip to content

Commit 0ed6f45

Browse files
committed
add new validator: ltField, lteField. add some tests
1 parent 396333c commit 0ed6f45

File tree

5 files changed

+81
-21
lines changed

5 files changed

+81
-21
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ $v = Validation::make($_POST,[
550550
`distinct` | 数组中的值必须是唯一的 | `['goods', 'distinct']`, `['users.*.id', 'distinct']`
551551
`ints/intList` | 验证字段值是否是一个 int list | `['tagIds', 'intList']`
552552
`numList` | 验证字段值是否是一个 number list | `['tagIds', 'numList']`
553-
`strList` | 验证字段值是否是一个 string list | `['tags', 'strList']`
553+
`strings/strList` | 验证字段值是否是一个 string list | `['tags', 'strList']`
554554
`arrList` | 验证字段值是否是一个 array list(多维数组) | `['tags', 'arrList']`
555555
`min` | 最小边界值验证 | `['title', 'min', 40]`
556556
`max` | 最大边界值验证 | `['title', 'max', 40]`
@@ -563,9 +563,13 @@ $v = Validation::make($_POST,[
563563
`notIn` | 枚举验证: 不包含 | `['status', 'notIn', [4,5,6]]`
564564
`inField` | 枚举验证: 字段值 存在于 另一个字段(anotherField)的值中 | `['field', 'inField', 'anotherField']`
565565
`eq/mustBe` | 必须是等于给定值 | `['status', 'mustBe', 1]`
566-
`neq/notBe` | 不能等于给定值 | `['status', 'notBe', 0]`
566+
`ne/neq/notBe` | 不能等于给定值 | `['status', 'notBe', 0]`
567567
`eqField` | 字段值比较: 相同 | `['passwd', 'eqField', 'repasswd']`
568568
`neqField` | 字段值比较: 不能相同 | `['userId', 'neqField', 'targetId']`
569+
`ltField` | 字段值比较: 小于 | `['field1', 'ltField', 'field2']`
570+
`lteField` | 字段值比较: 小于等于 | `['field1', 'lteField', 'field2']`
571+
`gtField` | 字段值比较: 大于 | `['field1', 'gtField', 'field2']`
572+
`gteField` | 字段值比较: 大于等于 | `['field1', 'gteField', 'field2']`
569573
`requiredIf` | 指定的其它字段( anotherField )值等于任何一个 `value` 时,此字段为 **必填**(ref laravel) | `['city', 'requiredIf', 'myCity', ['chengdu'] ]`
570574
`requiredUnless` | 指定的其它字段( anotherField )值等于任何一个 `value` 时,此字段为 **不必填**(ref laravel) | `['city', 'requiredUnless', 'myCity', ['chengdu'] ]`
571575
`requiredWith` | 指定的字段中的 _任意一个_ 有值且不为空,则此字段为 **必填**(ref laravel) | `['city', 'requiredWith', ['myCity'] ]`

src/Traits/ScopedValidatorsTrait.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,33 @@ public function neqFieldValidator($val, string $compareField): bool
437437
return $compareField && ($val !== $this->getByPath($compareField));
438438
}
439439

440-
// TODO: gtFiled, ltField, gteField, lteField
440+
// TODO: ltField, lteField
441+
442+
/**
443+
* 字段值比较:当前字段值 要小于 给定字段的值
444+
* @param string|int $val
445+
* @param string $compareField
446+
* @return bool
447+
*/
448+
public function ltFieldValidator($val, string $compareField): bool
449+
{
450+
$minVal = $this->getByPath($compareField);
451+
452+
return Validators::gt($val, $minVal);
453+
}
454+
455+
/**
456+
* 字段值比较:当前字段值 要小于等于 给定字段的值
457+
* @param string|int $val
458+
* @param string $compareField
459+
* @return bool
460+
*/
461+
public function lteFieldValidator($val, string $compareField): bool
462+
{
463+
$minVal = $this->getByPath($compareField);
464+
465+
return Validators::gte($val, $minVal);
466+
}
441467

442468
/**
443469
* 字段值比较:当前字段值 要大于 给定字段的值
@@ -572,7 +598,7 @@ public static function isCheckRequired(string $name): bool
572598
* @param string $field
573599
* @return array|null
574600
*/
575-
public function getUploadedFile($field)
601+
public function getUploadedFile(string $field)
576602
{
577603
return $this->uploadedFiles[$field] ?? null;
578604
}

src/Validator/Messages.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Class Messages
1313
* @package Inhere\Validate
1414
*/
15-
class Messages
15+
final class Messages
1616
{
1717
/**
1818
* Default error messages
@@ -54,8 +54,23 @@ class Messages
5454
// 'lengthEq', 'sizeEq'
5555
'fixedSize' => '{attr} length must is {value0}',
5656

57+
'eq' => '{attr} must be equals to {value0}',
58+
// 'different'
59+
'ne' => '{attr} can not be equals to {value0}',
5760
'min' => '{attr} minimum boundary is {value0}',
5861
'max' => '{attr} maximum boundary is {value0}',
62+
'lt' => '{attr} value must be less than {value0}',
63+
'lte' => '{attr} value must be less than or equals to {value0}',
64+
'gt' => '{attr} value must be greater than or equals to {value0}',
65+
'gte' => '{attr} value must be greater than or equals to {value0}',
66+
67+
// field compare
68+
'eqField' => '{attr} value must be less than {value0}',
69+
'neqField' => '{attr} value must be less than {value0}',
70+
'ltField' => '{attr} value must be less than {value0}',
71+
'lteField' => '{attr} value must be less than or equals to {value0}',
72+
'gtField' => '{attr} value must be greater than {value0}',
73+
'gteField' => '{attr} value must be greater than or equals to {value0}',
5974

6075
// 'in', 'enum',
6176
'enum' => '{attr} must in ({value0})',
@@ -73,12 +88,8 @@ class Messages
7388
'mustBe' => '{attr} must be equals to {value0}',
7489
'notBe' => '{attr} can not be equals to {value0}',
7590

76-
'compare' => '{attr} must be equals to {value0}',
77-
'same' => '{attr} must be equals to {value0}',
78-
'equal' => '{attr} must be equals to {value0}',
79-
80-
// 'different'
81-
'notEqual' => '{attr} can not be equals to {value0}',
91+
'compare' => '{attr} must be equals to {value0}',
92+
'same' => '{attr} must be equals to {value0}',
8293

8394
'isArray' => '{attr} must be an array',
8495
'isMap' => '{attr} must be an array and is key-value format',
@@ -135,7 +146,7 @@ public static function getDefault(): string
135146
* @param string $key
136147
* @param string|array $msg
137148
*/
138-
public static function setMessage(string $key, $msg)
149+
public static function set(string $key, $msg)
139150
{
140151
if ($key && $msg) {
141152
self::$messages[$key] = $msg;
@@ -148,7 +159,7 @@ public static function setMessage(string $key, $msg)
148159
public static function setMessages(array $messages)
149160
{
150161
foreach ($messages as $key => $value) {
151-
self::setMessage($key, $value);
162+
self::set($key, $value);
152163
}
153164
}
154165

src/Validators.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Validators
1818
/**
1919
* @var array
2020
*/
21-
private static $validatorAliases = [
21+
private static $aliases = [
2222
// alias => real name.
2323
'int' => 'integer',
2424
'num' => 'number',
@@ -27,22 +27,27 @@ class Validators
2727
'greaterThan' => 'gt',
2828
'lessThan' => 'lt',
2929
'mustBe' => 'eq',
30+
'equal' => 'eq',
3031
'notBe' => 'neq',
32+
'notEqual' => 'neq',
33+
'ne' => 'neq',
3134
'range' => 'size',
3235
'between' => 'size',
3336
'len' => 'length',
3437
'lenEq' => 'fixedSize',
3538
'lengthEq' => 'fixedSize',
3639
'sizeEq' => 'fixedSize',
40+
'neField' => 'neqField',
3741
'diff' => 'neqField',
38-
'notEqual' => 'neqField',
3942
'different' => 'neqField',
43+
'equalField' => 'eqField',
4044
'map' => 'isMap',
4145
'list' => 'isList',
4246
'array' => 'isArray',
4347
'absUrl' => 'absoluteUrl',
44-
'equalField' => 'eqField',
45-
// 'ints' => 'intList',
48+
'ints' => 'intList',
49+
'stringList' => 'strList',
50+
'strings' => 'strList',
4651
];
4752

4853
public function get(string $name)
@@ -57,15 +62,15 @@ public function get(string $name)
5762
*/
5863
public static function getRealName(string $validator): string
5964
{
60-
return self::$validatorAliases[$validator] ?? $validator;
65+
return self::$aliases[$validator] ?? $validator;
6166
}
6267

6368
/**
6469
* @return array
6570
*/
66-
public static function getValidatorAliases(): array
71+
public static function getAliases(): array
6772
{
68-
return self::$validatorAliases;
73+
return self::$aliases;
6974
}
7075

7176
/*******************************************************************************

test/Validator/MessagesTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,28 @@
88

99
namespace Inhere\ValidateTest\Validator;
1010

11+
use Inhere\Validate\Validator\Messages;
12+
use PHPUnit\Framework\TestCase;
13+
1114
/**
1215
* Class MessagesTest
1316
* @package Inhere\ValidateTest\Validator
1417
*/
15-
class MessagesTest
18+
class MessagesTest extends TestCase
1619
{
1720
public function testBasic()
1821
{
22+
Messages::setMessages([
23+
'key1' => 'val1',
24+
'key2' => 'val2',
25+
'key3' => '',
26+
]);
27+
28+
$this->assertNotEmpty(Messages::getMessages());
29+
$this->assertSame('val1', Messages::get('key1'));
30+
$this->assertTrue(Messages::has('key1'));
31+
$this->assertFalse(Messages::has('key3'));
1932

33+
$this->assertContains('validation is not through!', Messages::getDefault());
2034
}
2135
}

0 commit comments

Comments
 (0)