25
25
26
26
## 项目地址
27
27
28
- - ** github** https://github.com/inhere/php-console .git
29
- - ** git@osc** https://gitee.com/inhere/php-console .git
28
+ - ** github** https://github.com/inhere/php-validate .git
29
+ - ** git@osc** https://gitee.com/inhere/php-validate .git
30
30
31
31
** 注意:**
32
32
39
39
40
40
``` bash
41
41
composer require inhere/php-validate
42
- // composer require inhere/php-validate ^1.2
42
+ // composer require inhere/php-validate ^version // 指定版本
43
43
```
44
44
45
45
- 使用 composer.json
@@ -62,7 +62,7 @@ git clone https://gitee.com/inhere/php-validate.git // git@osc
62
62
63
63
## 使用
64
64
65
- <a name =" how-to-use " ></a >
65
+ <a name =" how-to-use1 " ></a >
66
66
### 方式 1: 创建一个新的class,并继承Validation
67
67
68
68
创建一个新的class,并继承 ` Inhere\Validate\Validation ` 。用于一个(或一系列相关)请求的验证, 相当于 laravel 的 表单请求验证
@@ -145,6 +145,7 @@ $safeData = $v->getSafeData(); // 验证通过的安全数据
145
145
$db->save($safeData);
146
146
```
147
147
148
+ <a name =" how-to-use2 " ></a >
148
149
### 方式 2: 直接使用类 Validation
149
150
150
151
需要快速简便的使用验证时,可直接使用 ` Inhere\Validate\Validation `
@@ -175,6 +176,7 @@ class SomeController
175
176
}
176
177
```
177
178
179
+ <a name =" how-to-use3 " ></a >
178
180
### 方式 3: 创建一个新的class,使用 ValidationTrait
179
181
180
182
创建一个新的class,并使用 Trait ` Inhere\Validate\ValidationTrait ` 。 此方式是高级自定义的使用方式, 可以方便的嵌入到其他类中
@@ -250,10 +252,12 @@ class UserController
250
252
}
251
253
```
252
254
253
-
254
255
## 添加自定义验证器
255
256
256
- - 在继承了 ` Inhere\Validate\Validation ` 的子类添加验证方法. 请看上面的 ** 使用方式1**
257
+ - 在继承了 ` Inhere\Validate\Validation ` 的子类添加验证方法. 请看上面的 [ 使用方式1] ( #how-to-use1 )
258
+
259
+ > 注意: 写在当前类里的验证器方法必须带有后缀 ` Validator ` , 以防止对内部的其他的方法造成干扰
260
+
257
261
- 通过 ` Validation::addValidator() ` 添加自定义验证器. e.g:
258
262
259
263
``` php
@@ -410,8 +414,21 @@ $v = Validation::make($_POST,[
410
414
``` php
411
415
['tagId,userId,freeTime', 'number', 'filter' => 'int'],
412
416
['field', 'validator', 'filter' => 'filter0|filter1...'],
417
+
418
+ // 需要自定义性更高时,可以使用数组。
419
+ ['field1', 'validator', 'filter' => [
420
+ 'string',
421
+ 'trim',
422
+ ['Class', 'method'],
423
+ ['Object', 'method'],
424
+ // 追加额外参数。 传入时,第一个参数总是要过滤的字段值,其余的依次追加
425
+ 'myFilter' => ['arg1', 'arg2'],
426
+ ]],
413
427
```
414
428
429
+ > 注意: 写在当前类里的过滤器方法必须带有后缀 ` Filter ` , 以防止对内部的其他的方法造成干扰
430
+
431
+
415
432
> 过滤器请参看 http://php.net/manual/zh/filter.filters.sanitize.php
416
433
417
434
### 一个完整的规则示例
@@ -565,18 +582,21 @@ public function get(string $key, $default = null)
565
582
` float ` | 过滤非法字符,保留` float ` 格式的数据 | ` ['price', 'float', 'filter' => 'float'], `
566
583
` string ` | 过滤非法字符并转换为` string ` 类型 | ` ['userId', 'number', 'filter' => 'string'], `
567
584
` trim ` | 去除首尾空白字符,支持数组。 | ` ['username', 'min', 4, 'filter' => 'trim'], `
568
- ` lowercase ` | 字符串转换为小写 | ` ['description', 'string', 'filter' => 'lowercase'], `
569
- ` uppercase ` | 字符串转换为大写 | ` ['title', 'string', 'filter' => 'uppercase'], `
570
- ` snakeCase ` | 字符串转换为蛇形风格 | ` ['title', 'string', 'filter' => 'snakeCase'], `
571
- ` camelCase ` | 字符串转换为驼峰风格 | ` ['title', 'string', 'filter' => 'camelCase'], `
585
+ ` lower/ lowercase` | 字符串转换为小写 | ` ['description', 'string', 'filter' => 'lowercase'], `
586
+ ` upper/ uppercase` | 字符串转换为大写 | ` ['title', 'string', 'filter' => 'uppercase'], `
587
+ ` snake/ snakeCase` | 字符串转换为蛇形风格 | ` ['title', 'string', 'filter' => 'snakeCase'], `
588
+ ` camel/ camelCase` | 字符串转换为驼峰风格 | ` ['title', 'string', 'filter' => 'camelCase'], `
572
589
` timestamp/strToTime ` | 字符串日期转换时间戳 | ` ['pulishedAt', 'number', 'filter' => 'strToTime'], `
573
590
` abs ` | 返回绝对值 | ` ['field', 'int', 'filter' => 'abs'], `
574
591
` url ` | URL 过滤,移除所有不符合 URL 的字符 | ` ['field', 'url', 'filter' => 'url'], `
575
592
` email ` | email 过滤,移除所有不符合 email 的字符 | ` ['field', 'email', 'filter' => 'email'], `
576
593
` encoded ` | 去除 URL 编码不需要的字符,与 ` urlencode() ` 函数很类似 | ` ['imgUrl', 'url', 'filter' => 'encoded'], `
594
+ ` clearTags/stripTags ` | 相当于使用 ` strip_tags() ` | ` ['content', 'string', 'filter' => 'clearTags'], `
577
595
` escape/specialChars ` | 相当于使用 ` htmlspecialchars() ` 转义数据 | ` ['content', 'string', 'filter' => 'specialChars'], `
578
596
` quotes ` | 应用 ` addslashes() ` 转义数据 | ` ['content', 'string', 'filter' => 'quotes'], `
579
597
598
+ > php 内置的函数可直接使用。 e.g ` string|ucfirst `
599
+
580
600
<a name =" built-in-validators " ></a >
581
601
## 内置的验证器
582
602
@@ -606,9 +626,9 @@ public function get(string $key, $default = null)
606
626
` length ` | 长度验证( 跟 ` size ` 差不多, 但只能验证 ` string ` , ` array ` 的长度 | ` ['username', 'length', 'min' => 5, 'max' => 20] `
607
627
` in/enum ` | 枚举验证 | ` ['status', 'in', [1,2,3] `
608
628
` notIn ` | 枚举验证 | ` ['status', 'notIn', [4,5,6]] `
609
- ` mustBe ` | 必须是等于给定值 | ` ['status', 'mustBe', 0 ] `
629
+ ` mustBe ` | 必须是等于给定值 | ` ['status', 'mustBe', 1 ] `
610
630
` notBe ` | 不能等于给定值 | ` ['status', 'notBe', 0] `
611
- ` compare/same/equal ` | 字段值比较 | ` ['passwd', 'compare', 'repasswd'] `
631
+ ` compare/same/equal ` | 字段值相同比较 | ` ['passwd', 'compare', 'repasswd'] `
612
632
` notEqual ` | 字段值不能相同比较 | ` ['passwd', 'notEqual', 'repasswd'] `
613
633
` required ` | 要求此字段/属性是必须的 | ` ['tagId, userId', 'required' ] `
614
634
` requiredIf ` | 指定的其它字段( anotherField )值等于任何一个 value 时,此字段为 ** 必填** | ` ['city', 'requiredIf', 'myCity', ['chengdu'] ] `
@@ -633,7 +653,7 @@ public function get(string $key, $default = null)
633
653
` md5 ` | 验证是否是 md5 格式的字符串 | ` ['passwd', 'md5'] `
634
654
` sha1 ` | 验证是否是 sha1 格式的字符串 | ` ['passwd', 'sha1'] `
635
655
` color ` | 验证是否是html color | ` ['backgroundColor', 'color'] `
636
- ` regex/regexp ` | 使用正则进行验证 | ` ['name', 'regexp', '/^\w+$/'] `
656
+ ` regex/regexp ` | 使用正则进行验证 | ` ['name', 'regexp', '/^\w+$/'] `
637
657
` safe ` | 用于标记字段是安全的,无需验证 | ` ['createdAt, updatedAt', 'safe'] `
638
658
639
659
### ` safe ` 验证器,标记属性/字段是安全的
@@ -661,13 +681,13 @@ $v = Validation::make($_POST, [
661
681
// ...
662
682
```
663
683
664
- ### 一些补充说明
684
+ ### (注意) 一些补充说明
665
685
666
686
- ** 请将 ` required* ` 系列规则写在规则列表的最前面**
667
687
- 关于布尔值验证
668
688
* 如果是 "1"、"true"、"on" 和 "yes",则返回 TRUE
669
689
* 如果是 "0"、"false"、"off"、"no" 和 "",则返回 FALSE
670
- - ` size/range ` ` length ` 可以只定义 min 最小值。 但是 ** 当定义了max 值时,必须同时定义最小值**
690
+ - ` size/range ` ` length ` 可以只定义 ` min ` 最小值。 但是 ** 当定义了 ` max ` 值时,必须同时定义最小值**
671
691
- 支持对数组的子级值验证
672
692
673
693
``` php
@@ -685,8 +705,8 @@ $v = Validation::make($_POST, [
685
705
['goods.pear', 'max', 30], //goods 下的 pear 值最大不能超过 30
686
706
```
687
707
688
- - 验证大小范围 ` int ` 是比较大小。 ` string ` 和 ` array ` 是检查长度。大小范围 是包含边界值的
689
708
- ` required* ` 系列规则参考自 laravel
709
+ - 验证大小范围 ` int ` 是比较大小。 ` string ` 和 ` array ` 是检查长度。大小范围 是包含边界值的
690
710
691
711
## 代码示例
692
712
0 commit comments