Skip to content

Commit 9098608

Browse files
committed
bug fixed, some update
1 parent 161a026 commit 9098608

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ public function get(string $key, $default = null)
481481
----------|-------------|------------
482482
`int/integer` | 验证是否是 int | `['userId', 'int']`
483483
`num/number` | 验证是否是 number | `['userId', 'number']`
484-
`bool` | 验证是否是 bool | `['open', 'bool']`
484+
`bool/boolean` | 验证是否是 bool | `['open', 'bool']`
485485
`float` | 验证是否是 float | `['price', 'float']`
486486
`string` | 验证是否是 string. 支持长度检查 | `['name', 'string']`, `['name', 'string', 'min'=>4, 'max'=>16]`
487487
`alpha` | 验证值是否仅包含字母字符 | `['name', 'alpha']`
@@ -492,8 +492,7 @@ public function get(string $key, $default = null)
492492
`isList` | 验证值是否是一个自然数组 list (key是从0自然增长的) | `['tags', 'isList']`
493493
`intList` | 验证字段值是否是一个 int list | `['tagIds', 'intList']`
494494
`strList` | 验证字段值是否是一个 string list | `['tags', 'strList']`
495-
`size` | 验证大小范围, 可以支持验证 `int`, `string`, `array` 数据类型 | `['tagId', 'size', 'min'=>4, 'max'=>567]`
496-
`range` | `size` 验证的别名 | 跟 `size` 一样
495+
`size/range` | 验证大小范围, 可以支持验证 `int`, `string`, `array` 数据类型 | `['tagId', 'size', 'min'=>4, 'max'=>567]`
497496
`length` | 长度验证( 跟 `size`差不多, 但只能验证 `string`, `array` 的长度 | ....
498497
`min` | 最小边界值验证 | `['title', 'min', 40]`
499498
`max` | 最大边界值验证 | `['title', 'max', 40]`

examples/index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343

4444
['goods.pear', 'max', 30], //
4545

46+
['goods', 'isList'], //
47+
4648
['notExistsField1', 'requiredWithout', 'notExistsField2'], //
4749
// ['notExistsField1', 'requiredWithout', 'existsField'], //
4850

src/ValidationTrait.php

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,14 @@ public function validate(array $onlyChecked = [], $stopOnError = null)
218218
foreach ($attrs as $attr) {
219219
$value = $this->getValue($attr);
220220

221-
// mark attribute is safe. not need validate. like. 'created_at'
222-
if ($validator === 'safe') {
223-
$this->_safeData[$attr] = $value;
221+
// 不在需要检查的列表内
222+
if ($onlyChecked && !in_array($attr, $onlyChecked, true)) {
224223
continue;
225224
}
226225

227-
// 不在需要检查的列表内
228-
if ($onlyChecked && !in_array($attr, $onlyChecked, true)) {
226+
// mark attribute is safe. not need validate. like. 'created_at'
227+
if ($validator === 'safe') {
228+
$this->_safeData[$attr] = $value;
229229
continue;
230230
}
231231

@@ -300,22 +300,23 @@ protected function requiredValidate($attr, $value, $validator, $args)
300300
$result = $this->required($attr);
301301

302302
// 其他 required* 方法
303-
} else {
303+
} elseif (method_exists($this, $validator)) {
304304
// 压入当前属性/字段名
305305
array_unshift($args, $attr);
306306

307307
$result = call_user_func_array([$this, $validator], $args);
308-
}
309-
310-
// failed
311-
if (!$result) {
312-
return false;
308+
} else {
309+
throw new \InvalidArgumentException("The validator [$validator] is not exists!");
313310
}
314311

315312
// validate success, save value to safeData
316-
$this->collectSafeValue($attr, $value);
313+
if ($result) {
314+
$this->collectSafeValue($attr, $value);
317315

318-
return true;
316+
return true;
317+
}
318+
319+
return false;
319320
}
320321

321322
/**
@@ -364,14 +365,14 @@ protected function doValidate($data, $attr, $value, $validator, $args)
364365
throw new \InvalidArgumentException('Validator type is error, must is String or Closure!');
365366
}
366367

367-
if (!call_user_func_array($callback, $args)) {
368-
return false;
369-
}
370-
371368
// validate success, save value to safeData
372-
$this->collectSafeValue($attr, $value);
369+
if (call_user_func_array($callback, $args)) {
370+
$this->collectSafeValue($attr, $value);
373371

374-
return true;
372+
return true;
373+
}
374+
375+
return false;
375376
}
376377

377378
/**
@@ -456,6 +457,11 @@ protected function collectRules()
456457
// return $this->_availableRules;
457458
}
458459

460+
/**
461+
* collect Safe Value
462+
* @param string $attr
463+
* @param mixed $value
464+
*/
459465
protected function collectSafeValue($attr, $value)
460466
{
461467
// 进行的是子级属性检查 eg: 'goods.apple'
@@ -720,6 +726,7 @@ public function lastError($onlyMsg = true)
720726
'num' => '{attr} must be an integer greater than 0!',
721727
'number' => '{attr} must be an integer greater than 0!',
722728
'bool' => '{attr} must be is boolean!',
729+
'boolean' => '{attr} must be is boolean!',
723730
'float' => '{attr} must be is float!',
724731
'url' => '{attr} is not a url address!',
725732
'email' => '{attr} is not a email address!',
@@ -741,6 +748,10 @@ public function lastError($onlyMsg = true)
741748
'compare' => '{attr} must be equals to {value0}',
742749
'same' => '{attr} must be equals to {value0}',
743750
'isArray' => '{attr} must be an array',
751+
'isMap' => '{attr} must be an array and is key-value format',
752+
'isList' => '{attr} must be an array of nature',
753+
'intList' => '{attr} must be an array and value is all integers',
754+
'strList' => '{attr} must be an array and value is all strings',
744755
'json' => '{attr} must be an json string',
745756
'callback' => '{attr} don\'t pass the test and verify!',
746757
'_' => '{attr} validation is not through!',

0 commit comments

Comments
 (0)