Skip to content

Commit 5d98b04

Browse files
committed
merge master update to current branch
1 parent 9ab44a9 commit 5d98b04

9 files changed

+300
-133
lines changed

README.md

+36-16
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
2626
## 项目地址
2727

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
3030

3131
**注意:**
3232

@@ -39,7 +39,7 @@
3939

4040
```bash
4141
composer require inhere/php-validate
42-
// composer require inhere/php-validate ^1.2
42+
// composer require inhere/php-validate ^version // 指定版本
4343
```
4444

4545
- 使用 composer.json
@@ -62,7 +62,7 @@ git clone https://gitee.com/inhere/php-validate.git // git@osc
6262

6363
## 使用
6464

65-
<a name="how-to-use"></a>
65+
<a name="how-to-use1"></a>
6666
### 方式 1: 创建一个新的class,并继承Validation
6767

6868
创建一个新的class,并继承 `Inhere\Validate\Validation`。用于一个(或一系列相关)请求的验证, 相当于 laravel 的 表单请求验证
@@ -145,6 +145,7 @@ $safeData = $v->getSafeData(); // 验证通过的安全数据
145145
$db->save($safeData);
146146
```
147147

148+
<a name="how-to-use2"></a>
148149
### 方式 2: 直接使用类 Validation
149150

150151
需要快速简便的使用验证时,可直接使用 `Inhere\Validate\Validation`
@@ -175,6 +176,7 @@ class SomeController
175176
}
176177
```
177178

179+
<a name="how-to-use3"></a>
178180
### 方式 3: 创建一个新的class,使用 ValidationTrait
179181

180182
创建一个新的class,并使用 Trait `Inhere\Validate\ValidationTrait`。 此方式是高级自定义的使用方式, 可以方便的嵌入到其他类中
@@ -250,10 +252,12 @@ class UserController
250252
}
251253
```
252254

253-
254255
## 添加自定义验证器
255256

256-
- 在继承了 `Inhere\Validate\Validation` 的子类添加验证方法. 请看上面的 **使用方式1**
257+
- 在继承了 `Inhere\Validate\Validation` 的子类添加验证方法. 请看上面的 [使用方式1](#how-to-use1)
258+
259+
> 注意: 写在当前类里的验证器方法必须带有后缀 `Validator`, 以防止对内部的其他的方法造成干扰
260+
257261
- 通过 `Validation::addValidator()` 添加自定义验证器. e.g:
258262

259263
```php
@@ -410,8 +414,21 @@ $v = Validation::make($_POST,[
410414
```php
411415
['tagId,userId,freeTime', 'number', 'filter' => 'int'],
412416
['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+
]],
413427
```
414428

429+
> 注意: 写在当前类里的过滤器方法必须带有后缀 `Filter`, 以防止对内部的其他的方法造成干扰
430+
431+
415432
> 过滤器请参看 http://php.net/manual/zh/filter.filters.sanitize.php
416433
417434
### 一个完整的规则示例
@@ -565,18 +582,21 @@ public function get(string $key, $default = null)
565582
`float` | 过滤非法字符,保留`float`格式的数据 | `['price', 'float', 'filter' => 'float'],`
566583
`string` | 过滤非法字符并转换为`string`类型 | `['userId', 'number', 'filter' => 'string'],`
567584
`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'],`
572589
`timestamp/strToTime` | 字符串日期转换时间戳 | `['pulishedAt', 'number', 'filter' => 'strToTime'],`
573590
`abs` | 返回绝对值 | `['field', 'int', 'filter' => 'abs'],`
574591
`url` | URL 过滤,移除所有不符合 URL 的字符 | `['field', 'url', 'filter' => 'url'],`
575592
`email` | email 过滤,移除所有不符合 email 的字符 | `['field', 'email', 'filter' => 'email'],`
576593
`encoded` | 去除 URL 编码不需要的字符,与 `urlencode()` 函数很类似 | `['imgUrl', 'url', 'filter' => 'encoded'],`
594+
`clearTags/stripTags` | 相当于使用 `strip_tags()` | `['content', 'string', 'filter' => 'clearTags'],`
577595
`escape/specialChars` | 相当于使用 `htmlspecialchars()` 转义数据 | `['content', 'string', 'filter' => 'specialChars'],`
578596
`quotes` | 应用 `addslashes()` 转义数据 | `['content', 'string', 'filter' => 'quotes'],`
579597

598+
> php 内置的函数可直接使用。 e.g `string|ucfirst`
599+
580600
<a name="built-in-validators"></a>
581601
## 内置的验证器
582602

@@ -606,9 +626,9 @@ public function get(string $key, $default = null)
606626
`length` | 长度验证( 跟 `size`差不多, 但只能验证 `string`, `array` 的长度 | `['username', 'length', 'min' => 5, 'max' => 20]`
607627
`in/enum` | 枚举验证 | `['status', 'in', [1,2,3]`
608628
`notIn` | 枚举验证 | `['status', 'notIn', [4,5,6]]`
609-
`mustBe` | 必须是等于给定值 | `['status', 'mustBe', 0]`
629+
`mustBe` | 必须是等于给定值 | `['status', 'mustBe', 1]`
610630
`notBe` | 不能等于给定值 | `['status', 'notBe', 0]`
611-
`compare/same/equal` | 字段值比较 | `['passwd', 'compare', 'repasswd']`
631+
`compare/same/equal` | 字段值相同比较 | `['passwd', 'compare', 'repasswd']`
612632
`notEqual` | 字段值不能相同比较 | `['passwd', 'notEqual', 'repasswd']`
613633
`required` | 要求此字段/属性是必须的 | `['tagId, userId', 'required' ]`
614634
`requiredIf` | 指定的其它字段( anotherField )值等于任何一个 value 时,此字段为 **必填** | `['city', 'requiredIf', 'myCity', ['chengdu'] ]`
@@ -633,7 +653,7 @@ public function get(string $key, $default = null)
633653
`md5` | 验证是否是 md5 格式的字符串 | `['passwd', 'md5']`
634654
`sha1` | 验证是否是 sha1 格式的字符串 | `['passwd', 'sha1']`
635655
`color` | 验证是否是html color | `['backgroundColor', 'color']`
636-
`regex/regexp` | 使用正则进行验证 | `['name', 'regexp', '/^\w+$/']`
656+
`regex/regexp` | 使用正则进行验证 | `['name', 'regexp', '/^\w+$/']`
637657
`safe` | 用于标记字段是安全的,无需验证 | `['createdAt, updatedAt', 'safe']`
638658

639659
### `safe` 验证器,标记属性/字段是安全的
@@ -661,13 +681,13 @@ $v = Validation::make($_POST, [
661681
// ...
662682
```
663683

664-
### 一些补充说明
684+
### (注意)一些补充说明
665685

666686
- **请将 `required*` 系列规则写在规则列表的最前面**
667687
- 关于布尔值验证
668688
* 如果是 "1"、"true"、"on" 和 "yes",则返回 TRUE
669689
* 如果是 "0"、"false"、"off"、"no" 和 "",则返回 FALSE
670-
- `size/range` `length` 可以只定义 min 最小值。 但是 **当定义了max 值时,必须同时定义最小值**
690+
- `size/range` `length` 可以只定义 `min` 最小值。 但是 **当定义了 `max` 值时,必须同时定义最小值**
671691
- 支持对数组的子级值验证
672692

673693
```php
@@ -685,8 +705,8 @@ $v = Validation::make($_POST, [
685705
['goods.pear', 'max', 30], //goods 下的 pear 值最大不能超过 30
686706
```
687707

688-
- 验证大小范围 `int` 是比较大小。 `string``array` 是检查长度。大小范围 是包含边界值的
689708
- `required*` 系列规则参考自 laravel
709+
- 验证大小范围 `int` 是比较大小。 `string``array` 是检查长度。大小范围 是包含边界值的
690710

691711
## 代码示例
692712

0 commit comments

Comments
 (0)