Skip to content

Commit 423f180

Browse files
committed
update:
- required: support check array key and object. `['goods.apple', 'required']` - errors stracture modify: `[['name' => 'filed name', 'msg' => 'error msg']]` - add filter: `bool` - add validator: `inField`
1 parent 3138013 commit 423f180

10 files changed

+270
-127
lines changed

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ $v = Validation::make($_POST,[
495495
-------|-------------|------------
496496
`abs` | 返回绝对值 | `['field', 'int', 'filter' => 'abs'],`
497497
`int/integer` | 过滤非法字符并转换为`int`类型 **支持数组** | `['userId', 'number', 'filter' => 'int'],`
498+
`bool/boolean` | 转换为 `bool` [关于bool值](#about-bool-value) | `['argee', 'bool']`
498499
`float` | 过滤非法字符,保留`float`格式的数据 | `['price', 'float', 'filter' => 'float'],`
499500
`string` | 过滤非法字符并转换为`string`类型 | `['userId', 'number', 'filter' => 'string'],`
500501
`trim` | 去除首尾空白字符,支持数组。 | `['username', 'min', 4, 'filter' => 'trim'],`
@@ -547,8 +548,9 @@ $v = Validation::make($_POST,[
547548
`fixedSize/sizeEq/lengthEq` | 固定的长度/大小(验证 `string`, `array` 长度, `int` 大小) | `['field', 'fixedSize', 12]`
548549
`startWith` | 值(`string/array`)是以给定的字符串开始 | `['field', 'startWith', 'hell']`
549550
`endWith` | 值(`string/array`)是以给定的字符串结尾 | `['field', 'endWith', 'world']`
550-
`in/enum` | 枚举验证包含 | `['status', 'in', [1,2,3]]`
551-
`notIn` | 枚举验证不包含 | `['status', 'notIn', [4,5,6]]`
551+
`in/enum` | 枚举验证: 包含 | `['status', 'in', [1,2,3]]`
552+
`notIn` | 枚举验证: 不包含 | `['status', 'notIn', [4,5,6]]`
553+
`inField` | 枚举验证: 字段值 存在于 另一个字段(anotherField)的值中 | `['field', 'inField', 'anotherField']`
552554
`mustBe` | 必须是等于给定值 | `['status', 'mustBe', 1]`
553555
`notBe` | 不能等于给定值 | `['status', 'notBe', 0]`
554556
`compare/same/equal` | 字段值相同比较 | `['passwd', 'compare', 'repasswd']`
@@ -609,11 +611,13 @@ $v = Validation::make($_POST, [
609611
- **请将 `required*` 系列规则写在规则列表的最前面**
610612
- 关于为空判断:字段符合下方任一条件时即为「空」<a name="about-empty-value"></a>
611613
- 该值为 `null`.
612-
- 该值为空字符串
613-
- 该值为空数组
614-
- 关于布尔值验证<a name="about-bool-value"></a>
615-
* 如果是 "1"、"true"、"on" 和 "yes",则返回 `TRUE`
616-
* 如果是 "0"、"false"、"off"、"no" 和 "",则返回 `FALSE`
614+
- 该值为空字符串 `''`
615+
- 该值为空数组 `[]`
616+
- 该值为空对象 -- 空的 `可数` 对象
617+
- 该值为没有路径的上传文件
618+
- 关于布尔值: 值符合下列的任意一项即认为是为bool值<a name="about-bool-value"></a>
619+
- 是 "1"、"true"、"on" 和 "yes" (`TRUE`)
620+
- 是 "0"、"false"、"off"、"no" 和 "" (`FALSE`)
617621
- `size/range` `length` 可以只定义 `min` 或者 `max`
618622
- 支持对数组的子级值验证
619623

@@ -695,9 +699,9 @@ public function getErrors(): array
695699

696700
```php
697701
[
698-
[ attr1 => 'error message 1'],
699-
[ attr1 => 'error message 2'],
700-
[ attr2 => 'error message 3'],
702+
['name' => 'field1', 'msg' => 'error Message1' ],
703+
['name' => 'field2', 'msg' => 'error Message2' ],
704+
...
701705
]
702706
```
703707

src/Filter/FilterList.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,40 @@
1414
*/
1515
final class FilterList
1616
{
17+
/**
18+
* 布尔值验证,转换成字符串后是下列的一个,就认为他是个bool值
19+
* - "1"、"true"、"on" 和 "yes" (equal TRUE)
20+
* - "0"、"false"、"off"、"no" 和 ""(equal FALSE)
21+
* 注意: NULL 不是标量类型
22+
* @param mixed $val
23+
* @param bool $nullAsFalse
24+
* @return bool
25+
*/
26+
public static function boolean($val, $nullAsFalse = false)
27+
{
28+
if ($val !== null && !is_scalar($val)) {
29+
return (bool)$val;
30+
}
31+
32+
return filter_var($val, FILTER_VALIDATE_BOOLEAN, [
33+
'flags' => $nullAsFalse ? FILTER_NULL_ON_FAILURE : 0
34+
]);
35+
}
36+
37+
/**
38+
* @see ValidatorList::boolean()
39+
* {@inheritdoc}
40+
*/
41+
public static function bool($val, $nullAsFalse = false)
42+
{
43+
return self::boolean($val, $nullAsFalse);
44+
}
45+
1746
/**
1847
* 过滤器删除数字中所有非法的字符。
1948
* @note 该过滤器允许所有数字以及 . + -
2049
* @param mixed $val 要过滤的变量
21-
* @return mixed $string
50+
* @return int
2251
*/
2352
public static function integer($val)
2453
{

src/Utils/DataFiltersTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected function valueFiltering($value, $filters)
6969
}
7070

7171
/**
72-
* @param $filter
72+
* @param mixed $filter
7373
* @param array ...$args
7474
* @return mixed
7575
*/

src/Utils/ErrorMessageTrait.php

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ trait ErrorMessageTrait
121121
* 保存所有的验证错误信息
122122
* @var array[]
123123
* [
124-
* [ field => errorMessage1 ],
125-
* [ field => errorMessage2 ],
126-
* [ field2 => errorMessage3 ],
124+
* ['name' => 'field', 'msg' => 'error Message1' ],
125+
* ['name' => 'field2', 'msg' => 'error Message2' ],
127126
* ]
128127
*/
129128
private $_errors = [];
@@ -198,19 +197,51 @@ public function isPassed(): bool
198197
}
199198

200199
/**
201-
* @param string $attr
200+
* check field whether in the errors
201+
* @param string $field
202+
* @return bool
203+
*/
204+
public function inError(string $field): bool
205+
{
206+
foreach ($this->_errors as $item) {
207+
if ($field === $item['name']) {
208+
return true;
209+
}
210+
}
211+
212+
return false;
213+
}
214+
215+
/**
216+
* @param string $field
202217
* @param string $msg
203218
*/
204-
public function addError(string $attr, string $msg)
219+
public function addError(string $field, string $msg)
205220
{
206-
$this->_errors[] = [$attr => $msg];
221+
$this->_errors[] = [
222+
'name' => $field,
223+
'msg' => $msg,
224+
];
207225
}
208226

209227
/**
228+
* @param string|null $field Only get errors of the field.
210229
* @return array
211230
*/
212-
public function getErrors(): array
231+
public function getErrors(string $field = null): array
213232
{
233+
if ($field) {
234+
$errors = [];
235+
236+
foreach ($this->_errors as $item) {
237+
if ($field === $item['name']) {
238+
$errors[] = $item['msg'];
239+
}
240+
}
241+
242+
return $errors;
243+
}
244+
214245
return $this->_errors;
215246
}
216247

@@ -235,7 +266,7 @@ public function firstError($onlyMsg = true)
235266
$e = $this->_errors;
236267
$first = array_shift($e);
237268

238-
return $onlyMsg ? array_values($first)[0] : $first;
269+
return $onlyMsg ? array_values($first)['msg'] : $first;
239270
}
240271

241272
/**
@@ -249,7 +280,7 @@ public function lastError($onlyMsg = true)
249280
$e = $this->_errors;
250281
$last = array_pop($e);
251282

252-
return $onlyMsg ? array_values($last)[0] : $last;
283+
return $onlyMsg ? array_values($last)['msg'] : $last;
253284
}
254285

255286
/**
@@ -373,12 +404,12 @@ public function getMessage($validator, $field, array $args = [], $message = null
373404

374405
/**
375406
* set the attrs translation data
376-
* @param array $attrTrans
407+
* @param array $fieldTrans
377408
* @return $this
378409
*/
379-
public function setTranslates(array $attrTrans)
410+
public function setTranslates(array $fieldTrans)
380411
{
381-
$this->_translates = $attrTrans;
412+
$this->_translates = $fieldTrans;
382413

383414
return $this;
384415
}
@@ -398,14 +429,14 @@ public function getTranslates(): array
398429
}
399430

400431
/**
401-
* @param string $attr
432+
* @param string $field
402433
* @return string
403434
*/
404-
public function getTranslate(string $attr): string
435+
public function getTranslate(string $field): string
405436
{
406437
$trans = $this->getTranslates();
407438

408-
return $trans[$attr] ?? Helper::beautifyFieldName($attr);
439+
return $trans[$field] ?? Helper::beautifyFieldName($field);
409440
}
410441

411442
/**

src/Utils/Helper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
*/
1515
class Helper
1616
{
17+
const IS_TRUE = '|yes|on|1|true|';
18+
const IS_FALSE = '|no|off|0|false|';
19+
const IS_BOOL = '|yes|on|1|true|no|off|0|false|';
20+
1721
/**
1822
* known image mime types
1923
* @link https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

0 commit comments

Comments
 (0)