Skip to content

Commit aab2457

Browse files
committed
update: add more filter alias name, Enhanced filter, allow append args. add more method for validation.
1 parent bc5d983 commit aab2457

File tree

8 files changed

+293
-112
lines changed

8 files changed

+293
-112
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,16 @@ $v = Validation::make($_POST,[
409409
```php
410410
['tagId,userId,freeTime', 'number', 'filter' => 'int'],
411411
['field', 'validator', 'filter' => 'filter0|filter1...'],
412+
413+
// 需要自定义性更高时,可以使用数组。
414+
['field1', 'validator', 'filter' => [
415+
'string',
416+
'trim',
417+
['Class', 'method'],
418+
['Object', 'method'],
419+
// 追加额外参数。 传入时,第一个参数总是要过滤的字段值,其余的依次追加
420+
'myFilter' => ['arg1', 'arg2'],
421+
]],
412422
```
413423

414424
> 过滤器请参看 http://php.net/manual/zh/filter.filters.sanitize.php
@@ -564,18 +574,21 @@ public function get(string $key, $default = null)
564574
`float` | 过滤非法字符,保留`float`格式的数据 | `['price', 'float', 'filter' => 'float'],`
565575
`string` | 过滤非法字符并转换为`string`类型 | `['userId', 'number', 'filter' => 'string'],`
566576
`trim` | 去除首尾空白字符,支持数组。 | `['username', 'min', 4, 'filter' => 'trim'],`
567-
`lowercase` | 字符串转换为小写 | `['description', 'string', 'filter' => 'lowercase'],`
568-
`uppercase` | 字符串转换为大写 | `['title', 'string', 'filter' => 'uppercase'],`
569-
`snakeCase` | 字符串转换为蛇形风格 | `['title', 'string', 'filter' => 'snakeCase'],`
570-
`camelCase` | 字符串转换为驼峰风格 | `['title', 'string', 'filter' => 'camelCase'],`
577+
`lower/lowercase` | 字符串转换为小写 | `['description', 'string', 'filter' => 'lowercase'],`
578+
`upper/uppercase` | 字符串转换为大写 | `['title', 'string', 'filter' => 'uppercase'],`
579+
`snake/snakeCase` | 字符串转换为蛇形风格 | `['title', 'string', 'filter' => 'snakeCase'],`
580+
`camel/camelCase` | 字符串转换为驼峰风格 | `['title', 'string', 'filter' => 'camelCase'],`
571581
`timestamp/strToTime` | 字符串日期转换时间戳 | `['pulishedAt', 'number', 'filter' => 'strToTime'],`
572582
`abs` | 返回绝对值 | `['field', 'int', 'filter' => 'abs'],`
573583
`url` | URL 过滤,移除所有不符合 URL 的字符 | `['field', 'url', 'filter' => 'url'],`
574584
`email` | email 过滤,移除所有不符合 email 的字符 | `['field', 'email', 'filter' => 'email'],`
575585
`encoded` | 去除 URL 编码不需要的字符,与 `urlencode()` 函数很类似 | `['imgUrl', 'url', 'filter' => 'encoded'],`
586+
`clearTags/stripTags` | 相当于使用 `strip_tags()` | `['content', 'string', 'filter' => 'clearTags'],`
576587
`escape/specialChars` | 相当于使用 `htmlspecialchars()` 转义数据 | `['content', 'string', 'filter' => 'specialChars'],`
577588
`quotes` | 应用 `addslashes()` 转义数据 | `['content', 'string', 'filter' => 'quotes'],`
578589

590+
> php 内置的函数可直接使用。 e.g `string|ucfirst`
591+
579592
<a name="built-in-validators"></a>
580593
## 内置的验证器
581594

src/FieldValidation.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Inhere\Validate;
1010

11+
use Inhere\Validate\Utils\Helper;
12+
1113
/**
1214
* Class FieldValidation
1315
* - one field to many rules. like Laravel framework
@@ -54,7 +56,7 @@ protected function collectRules()
5456

5557
// an rule for special scene.
5658
if (!empty($rule['on'])) {
57-
$sceneList = \is_string($rule['on']) ? array_map('trim', explode(',', $rule['on'])) : (array)$rule['on'];
59+
$sceneList = \is_string($rule['on']) ? Helper::explode($rule['on']) : (array)$rule['on'];
5860

5961
if ($scene && !\in_array($scene, $sceneList, true)) {
6062
continue;

0 commit comments

Comments
 (0)