Skip to content

Commit efdf83d

Browse files
committed
fix: required_without() premature return inside foreach loop
Bug: return statements inside the foreach loop caused only the first field in the otherFields list to be checked. All subsequent fields were silently ignored. - Refactored: foreach loop no longer contains early returns; each iteration independently determines whether to return false. - Added: null coalescing for field key access - Added: explicit is_array() check before array access to prevent warnings when dot_array_search() returns null - Added: validation for missing field key on dot-path fields
1 parent ac18654 commit efdf83d

1 file changed

Lines changed: 31 additions & 17 deletions

File tree

‎system/Validation/Rules.php‎

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -414,30 +414,44 @@ public function required_without(
414414
// Still here? Then we fail this test if
415415
// any of the fields are not present in $data
416416
foreach (explode(',', $otherFields) as $otherField) {
417-
if (
418-
(! str_contains($otherField, '.'))
419-
&& (! array_key_exists($otherField, $data)
420-
|| empty($data[$otherField]))
421-
) {
422-
return false;
417+
if (! str_contains($otherField, '.')) {
418+
if (! array_key_exists($otherField, $data) || empty($data[$otherField])) {
419+
return false;
420+
}
421+
422+
continue;
423423
}
424424

425-
if (str_contains($otherField, '.')) {
426-
if ($field === null) {
427-
throw new InvalidArgumentException('You must supply the parameters: field.');
428-
}
425+
if ($field === null) {
426+
throw new InvalidArgumentException('You must supply the parameters: field.');
427+
}
429428

430-
$fieldData = dot_array_search($otherField, $data);
431-
$fieldSplitArray = explode('.', $field);
432-
$fieldKey = $fieldSplitArray[1];
429+
$fieldSplitArray = explode('.', $field);
430+
$fieldKey = $fieldSplitArray[1] ?? null;
433431

434-
if (is_array($fieldData)) {
435-
return ! empty(dot_array_search($otherField, $data)[$fieldKey]);
432+
if ($fieldKey === null) {
433+
throw new InvalidArgumentException('Invalid field format for dot-path required_without.');
434+
}
435+
436+
$fieldData = dot_array_search($otherField, $data);
437+
438+
if (is_array($fieldData)) {
439+
$searched = dot_array_search($otherField, $data);
440+
441+
if (! is_array($searched) || ! array_key_exists($fieldKey, $searched)) {
442+
return false;
436443
}
444+
445+
if (empty($searched[$fieldKey])) {
446+
return false;
447+
}
448+
} else {
437449
$nowField = str_replace('*', $fieldKey, $otherField);
438-
$nowFieldVaule = dot_array_search($nowField, $data);
450+
$nowFieldValue = dot_array_search($nowField, $data);
439451

440-
return null !== $nowFieldVaule;
452+
if ($nowFieldValue === null) {
453+
return false;
454+
}
441455
}
442456
}
443457

0 commit comments

Comments
 (0)