3
3
[ ![ License] ( https://img.shields.io/packagist/l/inhere/php-validate.svg?style=flat-square )] ( LICENSE )
4
4
[ ![ Php Version] ( https://img.shields.io/badge/php-%3E=7.0-brightgreen.svg?maxAge=2592000 )] ( https://packagist.org/packages/inhere/php-validate )
5
5
[ ![ Latest Stable Version] ( http://img.shields.io/packagist/v/inhere/php-validate.svg )] ( https://packagist.org/packages/inhere/php-validate )
6
- [ ![ git branch] ( https://img.shields.io/badge/branch-php5 -yellow.svg )] ( https://github.com/inhere/php-validate )
6
+ [ ![ git branch] ( https://img.shields.io/badge/branch-master -yellow.svg )] ( https://github.com/inhere/php-validate )
7
7
8
8
一个简洁小巧且功能完善的php验证、过滤库。仅有几个文件,无依赖。
9
9
16
16
- 方便的获取错误信息,验证后的安全数据获取
17
17
- 已经内置了40多个常用的验证器[ 内置验证器] ( #built-in-validators )
18
18
- 规则设置参考自 yii 的。部分规则参考自 laravel
19
- - ` RuleValidation ` 规则配置类似于Yii: 每条规则中,允许多个字段,但只能有一个验证器。
19
+ - ` Validation/ RuleValidation` 规则配置类似于Yii: 每条规则中,允许多个字段,但只能有一个验证器。
20
20
- e.g ` ['tagId,userId,name,email,freeTime', 'required', ...] ` (下面的示例都是这种)
21
21
- ` FieldValidation ` 规则配置类似于Laravel: 每条规则中,只能有一个字段,但允许多个验证器。
22
22
- e.g ` ['field', 'required|string:5,10|...', ...] `
25
25
26
26
## 项目地址
27
27
28
- - ** git@osc** https://gitee.com/inhere/php-validate.git
29
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
35
35
36
36
## 安装
37
37
38
- - 使用 composer
38
+ - 使用 composer 命令
39
+
40
+ ``` php
41
+ composer require inhere/php-validate
42
+ // composer require inhere/php-validate ^2.2
43
+ ```
44
+
45
+ - 使用 composer.json
39
46
40
47
编辑 ` composer.json ` ,在 ` require ` 添加
41
48
49
56
- 直接拉取
50
57
51
58
```
52
- git clone https://git.oschina.net/inhere/php-validate.git // git@osc
53
59
git clone https://github.com/inhere/php-validate.git // github
60
+ git clone https://gitee.com/inhere/php-validate.git // git@osc
54
61
```
55
62
56
63
## 使用
@@ -63,7 +70,6 @@ git clone https://github.com/inhere/php-validate.git // github
63
70
> 此方式是最为完整的使用方式
64
71
65
72
``` php
66
-
67
73
use Inhere\Validate\Validation;
68
74
69
75
class PageRequest extends Validation
@@ -82,7 +88,7 @@ class PageRequest extends Validation
82
88
['username', 'string', 'on' => 'scene2' ],
83
89
['username', 'regexp' ,'/^[a-z]\w{2,12}$/'],
84
90
['title', 'customValidator', 'msg' => '{attr} error msg!' ], // 指定当前规则的消息
85
- ['status', function($status) {
91
+ ['status', function($status) { // 直接使用闭包验证
86
92
if (is_int($status) && $status > 3) {
87
93
return true;
88
94
}
@@ -108,7 +114,7 @@ class PageRequest extends Validation
108
114
];
109
115
}
110
116
111
- // 自定义验证器的提示消息, 更多请看 {@see ValidationTrait ::$messages}
117
+ // 自定义验证器的提示消息, 默认消息请看 {@see ErrorMessageTrait ::$messages}
112
118
public function messages()
113
119
{
114
120
return [
@@ -169,7 +175,7 @@ class SomeController
169
175
}
170
176
```
171
177
172
- ### 方式 1 : 创建一个新的class,使用 ValidationTrait
178
+ ### 方式 3 : 创建一个新的class,使用 ValidationTrait
173
179
174
180
创建一个新的class,并使用 Trait ` Inhere\Validate\ValidationTrait ` 。 此方式是高级自定义的使用方式, 可以方便的嵌入到其他类中
175
181
@@ -244,7 +250,6 @@ class UserController
244
250
}
245
251
```
246
252
247
-
248
253
## 添加自定义验证器
249
254
250
255
- 在继承了 ` Inhere\Validate\Validation ` 的子类添加验证方法. 请看上面的 ** 使用方式1**
@@ -559,8 +564,10 @@ public function get(string $key, $default = null)
559
564
` float ` | 过滤非法字符,保留` float ` 格式的数据 | ` ['price', 'float', 'filter' => 'float'], `
560
565
` string ` | 过滤非法字符并转换为` string ` 类型 | ` ['userId', 'number', 'filter' => 'string'], `
561
566
` trim ` | 去除首尾空白字符,支持数组。 | ` ['username', 'min', 4, 'filter' => 'trim'], `
562
- ` lowercase ` | 字符串转换为小写 | ` ['description', 'min', 4, 'filter' => 'lowercase'], `
563
- ` uppercase ` | 字符串转换为大写 | ` ['title', 'min', 4, 'filter' => 'uppercase'], `
567
+ ` lowercase ` | 字符串转换为小写 | ` ['description', 'string', 'filter' => 'lowercase'], `
568
+ ` uppercase ` | 字符串转换为大写 | ` ['title', 'string', 'filter' => 'uppercase'], `
569
+ ` snakeCase ` | 字符串转换为蛇形风格 | ` ['title', 'string', 'filter' => 'snakeCase'], `
570
+ ` camelCase ` | 字符串转换为驼峰风格 | ` ['title', 'string', 'filter' => 'camelCase'], `
564
571
` timestamp/strToTime ` | 字符串日期转换时间戳 | ` ['pulishedAt', 'number', 'filter' => 'strToTime'], `
565
572
` abs ` | 返回绝对值 | ` ['field', 'int', 'filter' => 'abs'], `
566
573
` url ` | URL 过滤,移除所有不符合 URL 的字符 | ` ['field', 'url', 'filter' => 'url'], `
@@ -590,6 +597,7 @@ public function get(string $key, $default = null)
590
597
` isList ` | 验证值是否是一个自然数组 list (key是从0自然增长的) | ` ['tags', 'isList'] `
591
598
` isArray ` | 验证是否是数组 | ` ['goods', 'isArray'] `
592
599
` intList ` | 验证字段值是否是一个 int list | ` ['tagIds', 'intList'] `
600
+ ` numList ` | 验证字段值是否是一个 number list | ` ['tagIds', 'numList'] `
593
601
` strList ` | 验证字段值是否是一个 string list | ` ['tags', 'strList'] `
594
602
` min ` | 最小边界值验证 | ` ['title', 'min', 40] `
595
603
` max ` | 最大边界值验证 | ` ['title', 'max', 40] `
@@ -624,7 +632,7 @@ public function get(string $key, $default = null)
624
632
` md5 ` | 验证是否是 md5 格式的字符串 | ` ['passwd', 'md5'] `
625
633
` sha1 ` | 验证是否是 sha1 格式的字符串 | ` ['passwd', 'sha1'] `
626
634
` color ` | 验证是否是html color | ` ['backgroundColor', 'color'] `
627
- ` regexp ` | 使用正则进行验证 | ` ['name', 'regexp', '/^\w+$/'] `
635
+ ` regex/ regexp` | 使用正则进行验证 | ` ['name', 'regexp', '/^\w+$/'] `
628
636
` safe ` | 用于标记字段是安全的,无需验证 | ` ['createdAt, updatedAt', 'safe'] `
629
637
630
638
### ` safe ` 验证器,标记属性/字段是安全的
@@ -658,6 +666,7 @@ $v = Validation::make($_POST, [
658
666
- 关于布尔值验证
659
667
* 如果是 "1"、"true"、"on" 和 "yes",则返回 TRUE
660
668
* 如果是 "0"、"false"、"off"、"no" 和 "",则返回 FALSE
669
+ - ` size/range ` ` length ` 可以只定义 ` min ` 最小值。 但是 ** 当定义了 ` max ` 值时,必须同时定义最小值**
661
670
- 支持对数组的子级值验证
662
671
663
672
``` php
@@ -676,9 +685,7 @@ $v = Validation::make($_POST, [
676
685
```
677
686
678
687
- ` required* ` 系列规则参考自 laravel
679
- - 验证大小范围 ` int ` 是比较大小。 ` string ` 和 ` array ` 是检查长度
680
- - ` size/range ` ` length ` 可以只定义 ` min ` 最小值。 但是 ** 当定义了 ` max ` 值时,必须同时定义最小值**
681
- - 验证大小范围 是包含边界值的
688
+ - 验证大小范围 ` int ` 是比较大小。 ` string ` 和 ` array ` 是检查长度。大小范围 是包含边界值的
682
689
683
690
## 代码示例
684
691
@@ -687,9 +694,23 @@ $v = Validation::make($_POST, [
687
694
## 单元测试
688
695
689
696
``` sh
690
- ./tests/test.sh
697
+ phpunit
691
698
```
692
699
693
700
## License
694
701
695
702
MIT
703
+
704
+ ## 我的其他项目
705
+
706
+ ### ` inhere/console ` [ github] ( https://github.com/inhere/php-console ) [ git@osc] ( https://git.oschina.net/inhere/php-console )
707
+
708
+ 轻量且功能丰富的命令行应用,工具库, 控制台交互.
709
+
710
+ ### ` inhere/sroute ` [ github] ( https://github.com/inhere/php-srouter ) [ git@osc] ( https://git.oschina.net/inhere/php-srouter )
711
+
712
+ 轻量且快速的路由库
713
+
714
+ ### ` inhere/http ` [ github] ( https://github.com/inhere/php-http ) [ git@osc] ( https://git.oschina.net/inhere/php-http )
715
+
716
+ http message 工具库(PSR 7 实现)
0 commit comments