diff --git a/README.md b/README.md index 4b4bbdd..d822e5a 100644 --- a/README.md +++ b/README.md @@ -657,14 +657,14 @@ $v = Validation::make($_POST, [ ```php [ -'goods' => [ - 'apple' => 34, - 'pear' => 50, - ], -'users' => [ - ['id' => 34, 'name' => 'tom'], - ['id' => 89, 'name' => 'john'], - ] + 'goods' => [ + 'apple' => 34, + 'pear' => 50, + ], + 'users' => [ + ['id' => 34, 'name' => 'tom'], + ['id' => 89, 'name' => 'john'], + ] ] ``` @@ -679,6 +679,8 @@ $v = Validation::make($_POST, [ ['users.*.name', 'each', 'string', 'min' => 5], ``` +> 对于带有通配符`*`的字段, 添加过滤器是无效的 + ## 一些关键方法API ### 设置验证场景 diff --git a/src/ValidationTrait.php b/src/ValidationTrait.php index 0d0312b..eb0b479 100644 --- a/src/ValidationTrait.php +++ b/src/ValidationTrait.php @@ -204,21 +204,21 @@ public function validate(array $onlyChecked = null, $stopOnError = null) } $value = $this->getByPath($field, $defValue); - // $hasWildcard = (bool)strpos($field, '*'); - // mark field is safe. not need validate. like. 'created_at' - if ($validator === 'safe') { - $this->setSafe($field, $value); - continue; - } - - // required* 系列字段检查器 || 文件资源检查 - if (\is_string($validator) && (self::isCheckRequired($validator) || self::isCheckFile($validator))) { - if (!$this->fieldValidate($field, $value, $validator, $args, $defMsg) && $this->isStopOnError()) { - break; + if (\is_string($validator)) { + if ($validator === 'safe') { + $this->setSafe($field, $value); + continue; } - continue; + // required*系列字段检查 || 文件资源检查 + if (self::isCheckRequired($validator) || self::isCheckFile($validator)) { + if (!$this->fieldValidate($field, $value, $validator, $args, $defMsg) && $this->isStopOnError()) { + break; + } + + continue; + } } // 设定了为空跳过 并且 值为空 @@ -226,8 +226,8 @@ public function validate(array $onlyChecked = null, $stopOnError = null) continue; } - // 字段值过滤 - if ($filters) { + // 字段值过滤(有通配符`*`的字段, 不应用过滤器) + if ($filters && !strpos($field, '.*')) { $value = $this->valueFiltering($value, $filters); $this->data[$field] = $value; }