Skip to content

fix: required_without() premature return inside foreach loop#10297

Closed
gr8man wants to merge 2 commits into
codeigniter4:developfrom
gr8man:fix/required-without-foreach-return
Closed

fix: required_without() premature return inside foreach loop#10297
gr8man wants to merge 2 commits into
codeigniter4:developfrom
gr8man:fix/required-without-foreach-return

Conversation

@gr8man

@gr8man gr8man commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Bug

In Rules::required_without(), the foreach loop over $otherFields
contained early return statements in the dot-path branch (lines 435, 440).
This caused only the first field in a comma-separated list to be evaluated;
all subsequent fields were silently ignored.

For example, required_without[a.*.b, a.*.c] would only check a.*.b
and return immediately, never inspecting a.*.c.

Fix

  • Refactored the foreach body so no iteration contains a return statement.
    Each field is independently evaluated, and false is returned only when
    a missing dependency is found.
  • Added ?? null coalescing for $fieldSplitArray[1] with a validation
    guard against malformed dot-path fields.
  • Added explicit is_array() check before array-key access on the result
    of dot_array_search(), preventing a PHP Warning when it returns null.

Tests

  • testRequireWithoutWithMultipleAsterisk – verifies that both a.*.b and
    a.*.d are checked when using required_without[a.*.b, a.*.d].
  • testRequireWithoutWithMultipleAsteriskLastMissing – verifies that
    validation fails when the last dot-path field is absent.

gr8man added 2 commits June 9, 2026 23:37
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
@gr8man gr8man force-pushed the fix/required-without-foreach-return branch from 4dd694a to 2cd2f69 Compare June 9, 2026 22:06
@gr8man gr8man closed this Jun 9, 2026
@gr8man gr8man deleted the fix/required-without-foreach-return branch June 9, 2026 22:09
@gr8man gr8man restored the fix/required-without-foreach-return branch June 9, 2026 22:32
@gr8man gr8man deleted the fix/required-without-foreach-return branch June 9, 2026 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant