@@ -155,18 +155,21 @@ public function required(string $field, $value = null): bool
155
155
}
156
156
157
157
/**
158
- * 如果指定的其它字段 ( anotherField )值等于任何一个 value 时,此字段为 必填
158
+ * 如果指定的另一个字段 ( anotherField )值等于任何一个 value 时,此字段为 必填
159
159
* @from laravel
160
160
* @param string $field
161
161
* @param mixed $fieldVal
162
162
* @param string $anotherField
163
163
* @param array|string $values
164
- * @return bool
164
+ * @return bool|null
165
+ * - TRUE check successful
166
+ * - FALSE check failed
167
+ * - NULL skip check
165
168
*/
166
- public function requiredIf (string $ field , $ fieldVal , string $ anotherField , $ values ): bool
169
+ public function requiredIf (string $ field , $ fieldVal , string $ anotherField , $ values )
167
170
{
168
171
if (!isset ($ this ->data [$ anotherField ])) {
169
- return false ;
172
+ return null ;
170
173
}
171
174
172
175
$ val = $ this ->data [$ anotherField ];
@@ -175,59 +178,56 @@ public function requiredIf(string $field, $fieldVal, string $anotherField, $valu
175
178
return $ this ->required ($ field , $ fieldVal );
176
179
}
177
180
178
- return false ;
181
+ return null ;
179
182
}
180
183
181
184
/**
182
- * 如果指定的其它字段( anotherField )值等于任何一个 value 时,此字段为 不必填
183
- * @from laravel
185
+ * 如果指定的另一个字段( anotherField )值等于任何一个 value 时,此字段为 不必填
184
186
* @param string $field
185
187
* @param mixed $fieldVal
186
188
* @param string $anotherField
187
189
* @param array|string $values
188
- * @return bool
190
+ * @return bool|null @see self::requiredIf()
189
191
*/
190
- public function requiredUnless (string $ field , $ fieldVal , string $ anotherField , $ values ): bool
192
+ public function requiredUnless (string $ field , $ fieldVal , string $ anotherField , $ values )
191
193
{
192
194
if (!isset ($ this ->data [$ anotherField ])) {
193
- return false ;
195
+ return $ this -> required ( $ field , $ fieldVal ) ;
194
196
}
195
197
196
198
if (\in_array ($ this ->data [$ anotherField ], (array )$ values , true )) {
197
- return true ;
199
+ return null ;
198
200
}
199
201
200
202
return $ this ->required ($ field , $ fieldVal );
201
203
}
202
204
203
205
/**
204
206
* 如果指定的其他字段中的 任意一个 有值且不为空,则此字段为 必填
205
- * @from laravel
206
207
* @param string $field
207
208
* @param mixed $fieldVal
208
209
* @param array|string $fields
209
- * @return bool
210
+ * @return bool|null @see self::requiredIf()
210
211
*/
211
- public function requiredWith (string $ field , $ fieldVal , $ fields ): bool
212
+ public function requiredWith (string $ field , $ fieldVal , $ fields )
212
213
{
213
214
foreach ((array )$ fields as $ name ) {
214
215
if ($ this ->required ($ name )) {
215
216
return $ this ->required ($ field , $ fieldVal );
216
217
}
217
218
}
218
219
219
- return true ;
220
+ return null ;
220
221
}
221
222
222
223
/**
223
- * 如果指定的 所有字段 都有值,则此字段为必填。
224
- * @from laravel
224
+ * 如果指定的 所有字段 都有值且不为空,则此字段为 必填
225
225
* @param string $field
226
226
* @param mixed $fieldVal
227
227
* @param array|string $fields
228
- * @return bool
228
+ * @return bool|null @see self::requiredIf()
229
229
*/
230
- public function requiredWithAll (string $ field , $ fieldVal , $ fields ): bool
230
+ public function requiredWithAll (string $ field , $ fieldVal , $ fields )
231
231
{
232
232
$ allHasValue = true ;
233
233
@@ -238,18 +238,17 @@ public function requiredWithAll(string $field, $fieldVal, $fields): bool
238
238
}
239
239
}
240
240
241
- return $ allHasValue ? $ this ->required ($ field , $ fieldVal ) : true ;
241
+ return $ allHasValue ? $ this ->required ($ field , $ fieldVal ) : null ;
242
242
}
243
243
244
244
/**
245
- * 如果缺少 任意一个 指定的字段值,则此字段为必填。
246
- * @from laravel
245
+ * 如果缺少 任意一个 指定的字段值,则此字段为 必填
247
246
* @param string $field
248
247
* @param mixed $fieldVal
249
248
* @param array|string $fields
250
- * @return bool
249
+ * @return bool|null @see self::requiredIf()
251
250
*/
252
- public function requiredWithout (string $ field , $ fieldVal , $ fields ): bool
251
+ public function requiredWithout (string $ field , $ fieldVal , $ fields )
253
252
{
254
253
$ allHasValue = true ;
255
254
@@ -260,18 +259,18 @@ public function requiredWithout(string $field, $fieldVal, $fields): bool
260
259
}
261
260
}
262
261
263
- return $ allHasValue ? true : $ this ->required ($ field , $ fieldVal );
262
+ return $ allHasValue ? null : $ this ->required ($ field , $ fieldVal );
264
263
}
265
264
266
265
/**
267
- * 如果所有指定的字段 都没有 值,则此字段为必填。
266
+ * 如果所有指定的字段 都没有 值,则此字段为 必填
268
267
* @from laravel
269
268
* @param string $field
270
269
* @param mixed $fieldVal
271
270
* @param array|string $fields
272
- * @return bool
271
+ * @return bool|null @see self::requiredIf()
273
272
*/
274
- public function requiredWithoutAll (string $ field , $ fieldVal , $ fields ): bool
273
+ public function requiredWithoutAll (string $ field , $ fieldVal , $ fields )
275
274
{
276
275
$ allNoValue = true ;
277
276
@@ -282,9 +281,13 @@ public function requiredWithoutAll(string $field, $fieldVal, $fields): bool
282
281
}
283
282
}
284
283
285
- return $ allNoValue ? $ this ->required ($ field , $ fieldVal ) : true ;
284
+ return $ allNoValue ? $ this ->required ($ field , $ fieldVal ) : null ;
286
285
}
287
286
287
+ /*******************************************************************************
288
+ * Files validators(require context data)
289
+ ******************************************************************************/
290
+
288
291
/**
289
292
* 验证的字段必须是成功上传的文件
290
293
* @param string $field
@@ -297,7 +300,7 @@ public function fileValidator(string $field, $suffixes = null): bool
297
300
return false ;
298
301
}
299
302
300
- if (!isset ($ file ['error ' ]) || ($ file ['error ' ] !== UPLOAD_ERR_OK )) {
303
+ if (!isset ($ file ['error ' ]) || ($ file ['error ' ] !== \ UPLOAD_ERR_OK )) {
301
304
return false ;
302
305
}
303
306
@@ -329,7 +332,7 @@ public function imageValidator(string $field, $suffixes = null): bool
329
332
return false ;
330
333
}
331
334
332
- if (!isset ($ file ['error ' ]) || ($ file ['error ' ] !== UPLOAD_ERR_OK )) {
335
+ if (!isset ($ file ['error ' ]) || ($ file ['error ' ] !== \ UPLOAD_ERR_OK )) {
333
336
return false ;
334
337
}
335
338
@@ -376,7 +379,7 @@ public function mimeTypesValidator(string $field, $types): bool
376
379
return false ;
377
380
}
378
381
379
- if (!isset ($ file ['error ' ]) || ($ file ['error ' ] !== UPLOAD_ERR_OK )) {
382
+ if (!isset ($ file ['error ' ]) || ($ file ['error ' ] !== \ UPLOAD_ERR_OK )) {
380
383
return false ;
381
384
}
382
385
@@ -477,7 +480,7 @@ public function inFieldValidator($val, string $anotherField): bool
477
480
*/
478
481
public function eachValidator (array $ values , ...$ args ): bool
479
482
{
480
- if (!$ validator = array_shift ($ args )) {
483
+ if (!$ validator = \ array_shift ($ args )) {
481
484
throw new \InvalidArgumentException ('must setting a validator for \'each \' validate. ' );
482
485
}
483
486
0 commit comments