Skip to content

Commit 48594b8

Browse files
committed
fix: fix the required* validators error
1 parent ac46a06 commit 48594b8

File tree

4 files changed

+66
-44
lines changed

4 files changed

+66
-44
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ class PageRequest extends Validation
107107
['userId', 'number', 'on' => 'scene1', 'filter' => 'int'],
108108
['username', 'string', 'on' => 'scene2', 'filter' => 'trim'],
109109
['username', 'regexp' ,'/^[a-z]\w{2,12}$/'],
110-
['title', 'customValidator', 'msg' => '{attr} error msg!' ], // 指定当前规则的消息
110+
// 自定义验证器,并指定当前规则的消息
111+
['title', 'custom', 'msg' => '{attr} error msg!' ],
111112
['status', function($status) { // 直接使用闭包验证
112113
if (is_int($status) && $status > 3) {
113114
return true;
@@ -316,8 +317,8 @@ $v = Validation::make($_POST,[
316317

317318
- 每条规则的第一个元素**必须**是 要验证的字段(可以同时配置多个,可以是数组. type:`string|array`)
318319
- 第二个元素**必须****一个**验证器(字符串,闭包,可回调的对象或数组. type:`string|Closure|callable`)
319-
- 后面紧跟着 是验证器可能需要的参数信息 (若验证器需要的参数只有一个,则参数无需带key)
320-
- 然后就是其他选项配置(msg,filter...)
320+
- 后面紧跟着 是验证器可能需要的参数信息 (若验证器需要的**参数只有一个,则参数无需带key**)
321+
- 然后就是其他选项配置(`msg`, `on`, `filter`, ...)
321322

322323
```php
323324
// a full rule
@@ -336,7 +337,7 @@ $v = Validation::make($_POST,[
336337
]
337338
```
338339

339-
> 字段验证器 `FieldValidation` 的配置类似,只是只有一个字段,而验证器允许有多个
340+
> 字段验证器 `FieldValidation` 的配置类似,只是 **只有一个字段,而验证器允许有多个**
340341
341342
## 规则关键词
342343

src/Utils/UserAndContextValidatorsTrait.php

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,21 @@ public function required(string $field, $value = null): bool
155155
}
156156

157157
/**
158-
* 如果指定的其它字段( anotherField )值等于任何一个 value 时,此字段为 必填
158+
* 如果指定的另一个字段( anotherField )值等于任何一个 value 时,此字段为 必填
159159
* @from laravel
160160
* @param string $field
161161
* @param mixed $fieldVal
162162
* @param string $anotherField
163163
* @param array|string $values
164-
* @return bool
164+
* @return bool|null
165+
* - TRUE check successful
166+
* - FALSE check failed
167+
* - NULL skip check
165168
*/
166-
public function requiredIf(string $field, $fieldVal, string $anotherField, $values): bool
169+
public function requiredIf(string $field, $fieldVal, string $anotherField, $values)
167170
{
168171
if (!isset($this->data[$anotherField])) {
169-
return false;
172+
return null;
170173
}
171174

172175
$val = $this->data[$anotherField];
@@ -175,59 +178,56 @@ public function requiredIf(string $field, $fieldVal, string $anotherField, $valu
175178
return $this->required($field, $fieldVal);
176179
}
177180

178-
return false;
181+
return null;
179182
}
180183

181184
/**
182-
* 如果指定的其它字段( anotherField )值等于任何一个 value 时,此字段为 不必填
183-
* @from laravel
185+
* 如果指定的另一个字段( anotherField )值等于任何一个 value 时,此字段为 不必填
184186
* @param string $field
185187
* @param mixed $fieldVal
186188
* @param string $anotherField
187189
* @param array|string $values
188-
* @return bool
190+
* @return bool|null @see self::requiredIf()
189191
*/
190-
public function requiredUnless(string $field, $fieldVal, string $anotherField, $values): bool
192+
public function requiredUnless(string $field, $fieldVal, string $anotherField, $values)
191193
{
192194
if (!isset($this->data[$anotherField])) {
193-
return false;
195+
return $this->required($field, $fieldVal);
194196
}
195197

196198
if (\in_array($this->data[$anotherField], (array)$values, true)) {
197-
return true;
199+
return null;
198200
}
199201

200202
return $this->required($field, $fieldVal);
201203
}
202204

203205
/**
204206
* 如果指定的其他字段中的 任意一个 有值且不为空,则此字段为 必填
205-
* @from laravel
206207
* @param string $field
207208
* @param mixed $fieldVal
208209
* @param array|string $fields
209-
* @return bool
210+
* @return bool|null @see self::requiredIf()
210211
*/
211-
public function requiredWith(string $field, $fieldVal, $fields): bool
212+
public function requiredWith(string $field, $fieldVal, $fields)
212213
{
213214
foreach ((array)$fields as $name) {
214215
if ($this->required($name)) {
215216
return $this->required($field, $fieldVal);
216217
}
217218
}
218219

219-
return true;
220+
return null;
220221
}
221222

222223
/**
223-
* 如果指定的 所有字段 都有值,则此字段为必填。
224-
* @from laravel
224+
* 如果指定的 所有字段 都有值且不为空,则此字段为 必填
225225
* @param string $field
226226
* @param mixed $fieldVal
227227
* @param array|string $fields
228-
* @return bool
228+
* @return bool|null @see self::requiredIf()
229229
*/
230-
public function requiredWithAll(string $field, $fieldVal, $fields): bool
230+
public function requiredWithAll(string $field, $fieldVal, $fields)
231231
{
232232
$allHasValue = true;
233233

@@ -238,18 +238,17 @@ public function requiredWithAll(string $field, $fieldVal, $fields): bool
238238
}
239239
}
240240

241-
return $allHasValue ? $this->required($field, $fieldVal) : true;
241+
return $allHasValue ? $this->required($field, $fieldVal) : null;
242242
}
243243

244244
/**
245-
* 如果缺少 任意一个 指定的字段值,则此字段为必填。
246-
* @from laravel
245+
* 如果缺少 任意一个 指定的字段值,则此字段为 必填
247246
* @param string $field
248247
* @param mixed $fieldVal
249248
* @param array|string $fields
250-
* @return bool
249+
* @return bool|null @see self::requiredIf()
251250
*/
252-
public function requiredWithout(string $field, $fieldVal, $fields): bool
251+
public function requiredWithout(string $field, $fieldVal, $fields)
253252
{
254253
$allHasValue = true;
255254

@@ -260,18 +259,18 @@ public function requiredWithout(string $field, $fieldVal, $fields): bool
260259
}
261260
}
262261

263-
return $allHasValue ? true : $this->required($field, $fieldVal);
262+
return $allHasValue ? null : $this->required($field, $fieldVal);
264263
}
265264

266265
/**
267-
* 如果所有指定的字段 都没有 值,则此字段为必填。
266+
* 如果所有指定的字段 都没有 值,则此字段为 必填
268267
* @from laravel
269268
* @param string $field
270269
* @param mixed $fieldVal
271270
* @param array|string $fields
272-
* @return bool
271+
* @return bool|null @see self::requiredIf()
273272
*/
274-
public function requiredWithoutAll(string $field, $fieldVal, $fields): bool
273+
public function requiredWithoutAll(string $field, $fieldVal, $fields)
275274
{
276275
$allNoValue = true;
277276

@@ -282,9 +281,13 @@ public function requiredWithoutAll(string $field, $fieldVal, $fields): bool
282281
}
283282
}
284283

285-
return $allNoValue ? $this->required($field, $fieldVal) : true;
284+
return $allNoValue ? $this->required($field, $fieldVal) : null;
286285
}
287286

287+
/*******************************************************************************
288+
* Files validators(require context data)
289+
******************************************************************************/
290+
288291
/**
289292
* 验证的字段必须是成功上传的文件
290293
* @param string $field
@@ -297,7 +300,7 @@ public function fileValidator(string $field, $suffixes = null): bool
297300
return false;
298301
}
299302

300-
if (!isset($file['error']) || ($file['error'] !== UPLOAD_ERR_OK)) {
303+
if (!isset($file['error']) || ($file['error'] !== \UPLOAD_ERR_OK)) {
301304
return false;
302305
}
303306

@@ -329,7 +332,7 @@ public function imageValidator(string $field, $suffixes = null): bool
329332
return false;
330333
}
331334

332-
if (!isset($file['error']) || ($file['error'] !== UPLOAD_ERR_OK)) {
335+
if (!isset($file['error']) || ($file['error'] !== \UPLOAD_ERR_OK)) {
333336
return false;
334337
}
335338

@@ -376,7 +379,7 @@ public function mimeTypesValidator(string $field, $types): bool
376379
return false;
377380
}
378381

379-
if (!isset($file['error']) || ($file['error'] !== UPLOAD_ERR_OK)) {
382+
if (!isset($file['error']) || ($file['error'] !== \UPLOAD_ERR_OK)) {
380383
return false;
381384
}
382385

@@ -477,7 +480,7 @@ public function inFieldValidator($val, string $anotherField): bool
477480
*/
478481
public function eachValidator(array $values, ...$args): bool
479482
{
480-
if (!$validator = array_shift($args)) {
483+
if (!$validator = \array_shift($args)) {
481484
throw new \InvalidArgumentException('must setting a validator for \'each\' validate.');
482485
}
483486

src/ValidationTrait.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ public function validate(array $onlyChecked = [], bool $stopOnError = null)
206206

207207
// required*系列字段检查 || 文件资源检查
208208
if (self::isCheckRequired($validator) || self::isCheckFile($validator)) {
209-
if (!$this->fieldValidate($field, $value, $validator, $args, $defMsg) && $this->isStopOnError()) {
209+
$result = $this->fieldValidate($field, $value, $validator, $args, $defMsg);
210+
211+
if (false === $result && $this->isStopOnError()) {
210212
break;
211213
}
212214

@@ -219,7 +221,7 @@ public function validate(array $onlyChecked = [], bool $stopOnError = null)
219221
continue;
220222
}
221223

222-
// 字段值过滤(有通配符`*`的字段, 不应用过滤器)
224+
// Field value filtering(有通配符`*`的字段, 不应用过滤器)
223225
if ($filters && !\strpos($field, '.*')) {
224226
$value = $this->valueFiltering($value, $filters);
225227
$this->data[$field] = $value;
@@ -261,10 +263,13 @@ public function validate(array $onlyChecked = [], bool $stopOnError = null)
261263
* @param string $validator required* 验证器
262264
* @param array $args Verify the required parameters
263265
* @param string|array $defMsg
264-
* @return bool
266+
* @return bool|null
267+
* - TRUE check successful
268+
* - FALSE check failed
269+
* - NULL skip check(for `required*`)
265270
* @throws \InvalidArgumentException
266271
*/
267-
protected function fieldValidate(string $field, $value, string $validator, array $args, $defMsg): bool
272+
protected function fieldValidate(string $field, $value, string $validator, array $args, $defMsg)
268273
{
269274
// required check
270275
if ($validator === 'required') {
@@ -288,9 +293,11 @@ protected function fieldValidate(string $field, $value, string $validator, array
288293
return true;
289294
}
290295

291-
$this->addError($field, $this->getMessage($validator, $field, $args, $defMsg));
296+
if ($passed === false) {
297+
$this->addError($field, $this->getMessage($validator, $field, $args, $defMsg));
298+
}
292299

293-
return false;
300+
return $passed;
294301
}
295302

296303
/**

test/RuleValidationTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,18 @@ public function testRequiredIf()
4444
$this->assertCount(1, $v->getErrors());
4545
$this->assertFalse($v->inError('userId'));
4646
$this->assertTrue($v->inError('targetId'));
47+
48+
$v = RuleValidation::check($data, [
49+
['userId, targetId', 'requiredIf', 'status', 5]
50+
]);
51+
52+
$this->assertCount(0, $v->getErrors());
53+
$this->assertCount(0, $v->getSafeData());
4754
}
4855

56+
/**
57+
* 如果指定的另一个字段( anotherField )值等于任何一个 value 时,此字段为 不必填
58+
*/
4959
public function testRequiredUnless()
5060
{
5161
$data = [
@@ -54,9 +64,10 @@ public function testRequiredUnless()
5464
'status' => 10,
5565
];
5666

57-
$v = RuleValidation::makeAndValidate($data, [
67+
$v = RuleValidation::check($data, [
5868
['targetId', 'requiredUnless', 'status', [10]],
5969
['userId', 'requiredUnless', 'status', [11]],
70+
['userId', 'requiredUnless', 'not-exists', [11]],
6071
]);
6172

6273
$this->assertCount(1, $v->getErrors());

0 commit comments

Comments
 (0)