Skip to content

Commit f45fbbd

Browse files
committed
some update ...
1 parent 5bfb625 commit f45fbbd

File tree

6 files changed

+115
-64
lines changed

6 files changed

+115
-64
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,7 @@ $v = Validation::make($_POST,[
357357
{
358358
return [
359359
['title', 'required' ],
360-
['tagId', 'number', 'when' => function($data)
361-
{
360+
['tagId', 'number', 'when' => function($data) {
362361
return isset($data['status']) && $data['status'] > 2;
363362
}],
364363
];
@@ -495,6 +494,7 @@ public function isFail() // hasError() 的别名方法
495494
public function fail() // hasError() 的别名方法
496495
497496
// 成功通过验证
497+
public function ok()
498498
public function passed()
499499
public function isPassed() // passed() 的别名方法
500500
```
@@ -624,7 +624,8 @@ public function get(string $key, $default = null)
624624
`max` | 最大边界值验证 | `['title', 'max', 40]`
625625
`size/range/between` | 验证大小范围, 可以支持验证 `int`, `string`, `array` 数据类型 | `['tagId', 'size', 'min'=>4, 'max'=>567]`
626626
`length` | 长度验证( 跟 `size`差不多, 但只能验证 `string`, `array` 的长度 | `['username', 'length', 'min' => 5, 'max' => 20]`
627-
`in/enum` | 枚举验证 | `['status', 'in', [1,2,3]`
627+
`fixedSize/fixedLength` | 固定的长度/大小 | `['field', 'fixedSize', 12]`
628+
`in/enum` | 枚举验证 | `['status', 'in', [1,2,3]]`
628629
`notIn` | 枚举验证 | `['status', 'notIn', [4,5,6]]`
629630
`mustBe` | 必须是等于给定值 | `['status', 'mustBe', 1]`
630631
`notBe` | 不能等于给定值 | `['status', 'notBe', 0]`

examples/DataModel.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class DataModel
1111

1212
protected $data = [];
1313

14+
protected $db;
15+
1416
/**
1517
* @param array $data
1618
* @return $this
@@ -21,4 +23,13 @@ public function setData($data)
2123

2224
return $this;
2325
}
26+
27+
public function create()
28+
{
29+
if ($this->validate()->fail()) {
30+
return false;
31+
}
32+
33+
return $this->db->insert($this->getSafeData());
34+
}
2435
}

examples/filter.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: inhere
5-
* Date: 2017-11-24
6-
* Time: 18:13
7-
*/
2+
3+
require __DIR__ . '/simple-loader.php';
4+
5+
$data = [
6+
'name' => ' tom ',
7+
'status' => ' 23 ',
8+
'word' => 'word',
9+
'toLower' => 'WORD',
10+
'title' => 'helloWorld',
11+
];
12+
13+
$rules = [
14+
['name', 'string|trim'],
15+
['status', 'trim|int'],
16+
['word', 'string|trim|upper'],
17+
['toLower', 'lower'],
18+
['title', [
19+
'string',
20+
'snake' => ['-'],
21+
'ucfirst',
22+
]],
23+
];
24+
25+
echo "------------- raw data: -------------\n";
26+
var_dump($data);
27+
28+
$cleaned = \Inhere\Validate\Filter\Filtration::make($data, $rules)->filtering();
29+
30+
echo "------------- cleaned data: -------------\n";
31+
var_dump($cleaned);

src/Utils/ErrorMessageTrait.php

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ trait ErrorMessageTrait
5555
'{attr} must be an integer/string/array and minimum value/length is {min}',
5656
'{attr} must be an integer/string/array and value/length between {min} ~ {max}',
5757
],
58+
'fixedSize' => '{attr} length must is {value0}',
5859
'min' => '{attr} minimum boundary is {value0}',
5960
'max' => '{attr} maximum boundary is {value0}',
6061
'in' => '{attr} must in ({value0})',
@@ -104,7 +105,7 @@ trait ErrorMessageTrait
104105
* [
105106
* [ field => errorMessage1 ],
106107
* [ field => errorMessage2 ],
107-
* [ field2 => errorMessage3 ]
108+
* [ field2 => errorMessage3 ],
108109
* ]
109110
*/
110111
private $_errors = [];
@@ -121,16 +122,6 @@ trait ErrorMessageTrait
121122
* Errors Information
122123
******************************************************************************/
123124

124-
/**
125-
* @return $this
126-
*/
127-
public function clearErrors()
128-
{
129-
$this->_errors = [];
130-
131-
return $this;
132-
}
133-
134125
/**
135126
* 是否有错误
136127
* @return boolean
@@ -164,6 +155,14 @@ public function failed(): bool
164155
return $this->isFail();
165156
}
166157

158+
/**
159+
* @return bool
160+
*/
161+
public function ok(): bool
162+
{
163+
return !$this->isFail();
164+
}
165+
167166
/**
168167
* @return bool
169168
*/
@@ -197,6 +196,16 @@ public function getErrors(): array
197196
return $this->_errors;
198197
}
199198

199+
/**
200+
* @return $this
201+
*/
202+
public function clearErrors()
203+
{
204+
$this->_errors = [];
205+
206+
return $this;
207+
}
208+
200209
/**
201210
* 得到第一个错误信息
202211
* @author inhere
@@ -380,18 +389,4 @@ public function getTranslate(string $attr): string
380389

381390
return $trans[$attr] ?? Helper::beautifyFieldName($attr);
382391
}
383-
384-
/**
385-
* set the attrs translation data
386-
* @deprecated please use setTranslates() instead.
387-
* @param array $attrTrans
388-
* @return $this
389-
*/
390-
public function setAttrTrans(array $attrTrans)
391-
{
392-
$this->_translates = $attrTrans;
393-
394-
return $this;
395-
}
396-
397392
}

src/ValidationTrait.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function messages()
8888
return [
8989
// validator name => message string
9090
// 'required' => '{attr} 是必填项。',
91-
// 'required.username' => '用户名 是必填项。',
91+
// 'username.required' => '用户名 是必填项。',
9292
];
9393
}
9494

@@ -178,17 +178,16 @@ public function validate(array $onlyChecked = null, $stopOnError = null)
178178

179179
// 验证的前置条件 -- 不满足条件,跳过此条规则
180180
$when = $rule['when'] ?? null;
181-
if ($when && $when instanceof \Closure && $when($data, $this) !== true) {
181+
if ($when && ($when instanceof \Closure) && $when($data, $this) !== true) {
182182
continue;
183183
}
184184

185185
// clear all keywords options
186186
unset($rule['msg'], $rule['default'], $rule['skipOnEmpty'], $rule['isEmpty'], $rule['when'], $rule['filter']);
187187

188-
// 验证设置, 有一些验证器需要参数。 e.g. size()
188+
// 验证器参数, 有一些验证器需要参数。 e.g. size()
189189
$args = $rule;
190190

191-
// 循环检查属性
192191
foreach ($fields as $field) {
193192
if (!$field || ($onlyChecked && !\in_array($field, $onlyChecked, true))) {
194193
continue;

src/ValidatorList.php

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -277,28 +277,6 @@ public static function range($val, $min = null, $max = null)
277277
return self::size($val, $min, $max);
278278
}
279279

280-
/**
281-
* 必须是等于给定值
282-
* @param mixed $val
283-
* @param mixed $excepted
284-
* @return bool
285-
*/
286-
public static function mustBe($val, $excepted)
287-
{
288-
return $val === $excepted;
289-
}
290-
291-
/**
292-
* 不能等于给定值
293-
* @param mixed $val
294-
* @param mixed $excepted
295-
* @return bool
296-
*/
297-
public static function notBe($val, $excepted)
298-
{
299-
return $val !== $excepted;
300-
}
301-
302280
/**
303281
* 最小值检查
304282
* @param int|string|array $val
@@ -337,6 +315,37 @@ public static function length($val, $minLength = 0, $maxLength = null)
337315
return self::size($val, $minLength, $maxLength);
338316
}
339317

318+
/**
319+
* 固定的长度
320+
* @param mixed $val
321+
* @param int $size
322+
* @return bool
323+
*/
324+
public static function fixedLength($val, $size)
325+
{
326+
return self::fixedSize($val, $size);
327+
}
328+
329+
/**
330+
* @param mixed $val
331+
* @param int $size
332+
* @return bool
333+
*/
334+
public static function fixedSize($val, $size)
335+
{
336+
if (!\is_int($val)) {
337+
if (\is_string($val)) {
338+
$val = Helper::strlen(trim($val));
339+
} elseif (\is_array($val)) {
340+
$val = \count($val);
341+
} else {
342+
return false;
343+
}
344+
}
345+
346+
return $val === (int)$size;
347+
}
348+
340349
/*******************************************************************************
341350
* custom validators
342351
******************************************************************************/
@@ -650,13 +659,25 @@ public static function notIn($val, $dict)
650659
}
651660

652661
/**
653-
* @param mixed $val
654-
* @param mixed $compareVal
662+
* 必须是等于给定值
663+
* @param mixed $val
664+
* @param mixed $excepted
655665
* @return bool
656666
*/
657-
public static function compare($val, $compareVal)
667+
public static function mustBe($val, $excepted)
658668
{
659-
return $val === $compareVal;
669+
return $val === $excepted;
670+
}
671+
672+
/**
673+
* 不能等于给定值
674+
* @param mixed $val
675+
* @param mixed $excepted
676+
* @return bool
677+
*/
678+
public static function notBe($val, $excepted)
679+
{
680+
return $val !== $excepted;
660681
}
661682

662683
/*******************************************************************************

0 commit comments

Comments
 (0)