@@ -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
0 commit comments