@@ -45,7 +45,7 @@ validate 同时支持两种规则配置方式,对应了两种规则的收集
45
45
``` php
46
46
[
47
47
['field', 'required|string:5,10|...', ...],
48
- // ... ...
48
+ // ... ...
49
49
]
50
50
```
51
51
@@ -115,45 +115,45 @@ class PageRequest extends Validation
115
115
return [
116
116
// 字段必须存在且不能为空
117
117
['tagId,title,userId,freeTime', 'required'],
118
-
118
+
119
119
// 4<= tagId <=567
120
- ['tagId', 'size', 'min'=>4, 'max'=>567, 'filter' => 'int'],
121
-
120
+ ['tagId', 'size', 'min'=>4, 'max'=>567, 'filter' => 'int'],
121
+
122
122
// title length >= 40. 注意只需一个参数的验证,无需加 key, 如这里的 40
123
123
['title', 'min', 40, 'filter' => 'trim'],
124
-
124
+
125
125
// 大于 0
126
126
['freeTime', 'number'],
127
-
127
+
128
128
// 含有前置条件
129
129
['tagId', 'number', 'when' => function($data) {
130
130
return isset($data['status']) && $data['status'] > 2;
131
131
}],
132
-
132
+
133
133
// 在验证前会先过滤转换为 int。并且仅会在指明场景名为 'scene1' 时规则有效
134
134
['userId', 'number', 'on' => 'scene1', 'filter' => 'int'],
135
135
['username', 'string', 'on' => 'scene2', 'filter' => 'trim'],
136
-
136
+
137
137
// 使用自定义正则表达式
138
138
['username', 'regexp' ,'/^[a-z]\w{2,12}$/'],
139
-
139
+
140
140
// 自定义验证器,并指定当前规则的消息
141
- ['title', 'custom', 'msg' => '{attr} error msg!' ],
142
-
141
+ ['title', 'custom', 'msg' => '{attr} error msg!' ],
142
+
143
143
// 直接使用闭包验证
144
- ['status', function($status) {
144
+ ['status', function($status) {
145
145
if (is_int($status) && $status > 3) {
146
146
return true;
147
147
}
148
148
return false;
149
149
}],
150
-
150
+
151
151
// 标记字段是安全可靠的 无需验证
152
152
['createdAt, updatedAt', 'safe'],
153
153
];
154
154
}
155
-
156
- // 定义不同场景需要验证的字段。
155
+
156
+ // 定义不同场景需要验证的字段。
157
157
// 功能跟规则里的 'on' 类似,两者尽量不要同时使用,以免混淆。
158
158
public function scenarios(): array
159
159
{
@@ -180,7 +180,7 @@ class PageRequest extends Validation
180
180
'title.required' => 'O, 标题是必填项。are you known?',
181
181
];
182
182
}
183
-
183
+
184
184
// 添加一个验证器。必须返回一个布尔值标明验证失败或成功
185
185
protected function customValidator($title): bool
186
186
{
@@ -270,7 +270,7 @@ class UserModel extends DataModel
270
270
}
271
271
272
272
// on controller action ...
273
- class UserController
273
+ class UserController
274
274
{
275
275
// in action
276
276
// @api /user/add
@@ -319,7 +319,7 @@ $v = Validation::make($_POST,[
319
319
if ($status > 3) {
320
320
return true;
321
321
}
322
-
322
+
323
323
return false;
324
324
}]
325
325
```
@@ -328,7 +328,7 @@ $v = Validation::make($_POST,[
328
328
329
329
一个完整的规则示例, 包含了所有可添加的项。
330
330
331
- ** 注意:**
331
+ ** 注意:**
332
332
333
333
- 每条规则的第一个元素** 必须** 是 _ 要验证的字段_ (可以同时配置多个,可以是数组. type:` string|array ` )
334
334
- 第二个元素** 必须** 是** 一个** 验证器(字符串,闭包,可回调的对象或数组. type:` string|Closure|callable ` )
@@ -339,15 +339,15 @@ $v = Validation::make($_POST,[
339
339
// a full rule
340
340
[
341
341
// basic validate setting
342
- 'field0,field1,...', 'validator', 'arg0', 'arg1', ...,
342
+ 'field0,field1,...', 'validator', 'arg0', 'arg1', ...,
343
343
344
344
// some extended option settings
345
- 'skipOnEmpty' => 'bool',
346
- 'msg' => 'string|array',
347
- 'default' => 'mixed',
348
- 'on' => 'string|array'
349
- 'isEmpty' => 'callback(string|closure)',
350
- 'when' => 'callback(string|closure)',
345
+ 'skipOnEmpty' => 'bool',
346
+ 'msg' => 'string|array',
347
+ 'default' => 'mixed',
348
+ 'on' => 'string|array'
349
+ 'isEmpty' => 'callback(string|closure)',
350
+ 'when' => 'callback(string|closure)',
351
351
'filter' => 'callback(string|array|closure)'
352
352
]
353
353
```
@@ -444,7 +444,7 @@ $v = Validation::make($_POST,[
444
444
提交的数据中 没有 ` name ` 字段或者 ` $data['name'] ` 等于空都不会进行 ` string ` 验证;
445
445
只有当 ` $data['name'] ` ** 有值且不为空** 时才会验证是否是 ` string `
446
446
447
- 如果要想为空时也检查, 请将此字段同时加入 ` required ` 规则中.
447
+ 如果要想为空时也检查, 请将此字段同时加入 ` required ` 规则中.
448
448
449
449
``` php
450
450
['name', 'required' ]
@@ -471,6 +471,17 @@ $v = Validation::make($_POST,[
471
471
}]
472
472
```
473
473
474
+ 自定义 isEmpty 验证时,应留意 $value 是否为 ArrayValueNotExists 实例
475
+
476
+ ``` php
477
+ ['users.*.id', 'each', 'required', 'isEmpty' => function($value) {
478
+ if ($value instanceof \Inhere\Validate\ArrayValueNotExists) {
479
+ return true;
480
+ }
481
+ // your code here ...
482
+ }]
483
+ ```
484
+
474
485
### ` filter ` -- 使用过滤器
475
486
476
487
支持在进行验证前对值使用过滤器进行净化过滤[ 内置过滤器] ( #built-in-filters )
@@ -641,7 +652,7 @@ $v = Validation::make($_POST,[
641
652
642
653
``` php
643
654
$v = Validation::make($_POST, [
644
- // [...],
655
+ // [...],
645
656
// some rules ...
646
657
])
647
658
->setUploadedFiles($_FILES)
@@ -653,9 +664,9 @@ $v = Validation::make($_POST, [
653
664
654
665
- ** 请将 ` required* ` 系列规则写在规则列表的最前面**
655
666
- 规则上都支持添加过滤器
656
- - 验证大小范围 ` int ` 是比较大小。 ` string ` 和 ` array ` 是检查长度。大小范围 是包含边界值的
667
+ - 验证大小范围 ` int ` 是比较大小。 ` string ` 和 ` array ` 是检查长度。大小范围 是包含边界值的
657
668
- ` size/range ` ` length ` 可以只定义 ` min ` 或者 ` max ` 值
658
- - 支持对数组的子级值验证
669
+ - 支持对数组的子级值验证
659
670
660
671
``` php
661
672
[
@@ -672,7 +683,7 @@ $v = Validation::make($_POST, [
672
683
['goods.pear', 'max', 30], //goods 下的 pear 值最大不能超过 30
673
684
```
674
685
675
- - 支持对数组的子级值进行遍历验证
686
+ - 支持对数组的子级值进行遍历验证
676
687
677
688
``` php
678
689
[
@@ -691,7 +702,7 @@ $v = Validation::make($_POST, [
691
702
['goods.*', 'each', 'number'], //goods 下的 每个值 都必须为大于0 的整数
692
703
// 写法是等效的
693
704
// ['goods', 'each', 'number'], //goods 下的 每个值 都必须为大于0 的整数
694
-
705
+
695
706
// 多维数组
696
707
['users.*.id', 'each', 'required'],
697
708
['users.*.id', 'each', 'number', 'min' => 34],
@@ -756,7 +767,7 @@ public function getErrors(): array
756
767
757
768
获取所有的错误信息, 包含所有错误的字段和错误信息的多维数组。 eg:
758
769
759
- ``` php
770
+ ``` php
760
771
[
761
772
['name' => 'field1', 'msg' => 'error Message1' ],
762
773
['name' => 'field2', 'msg' => 'error Message2' ],
@@ -788,7 +799,7 @@ public function lastError($onlyMsg = true)
788
799
public function getSafeData(): array|\stdClass
789
800
```
790
801
791
- 获取所有 ** 验证通过** 的安全数据.
802
+ 获取所有 ** 验证通过** 的安全数据.
792
803
793
804
- 此数据数组只包含加入了规则验证的字段数据,不会含有额外的字段。(可直接省去后续的字段收集)
794
805
- 推荐使用此数据进行后续操作,比如存入数据库等。
@@ -800,7 +811,7 @@ public function getSafeData(): array|\stdClass
800
811
``` php
801
812
public function val(string $key, $default = null) // getSafe() 的别名方法
802
813
public function getValid(string $key, $default = null) // getSafe() 的别名方法
803
- public function getSafe(string $key, $default = null)
814
+ public function getSafe(string $key, $default = null)
804
815
```
805
816
806
817
从 ** 验证通过** 的数据中取出对应 key 的值
@@ -823,7 +834,7 @@ public function getRaw(string $key, $default = null)
823
834
824
835
## 代码示例
825
836
826
- 可运行示例请看 ` example `
837
+ 可运行示例请看 ` example `
827
838
828
839
## 单元测试
829
840
0 commit comments