Skip to content

Commit 60fca7f

Browse files
committed
fix(Validation): correct required_without logic and prevent array key warnings
1 parent b08160c commit 60fca7f

3 files changed

Lines changed: 57 additions & 4 deletions

File tree

system/Validation/Rules.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,15 +429,22 @@ public function required_without(
429429

430430
$fieldData = dot_array_search($otherField, $data);
431431
$fieldSplitArray = explode('.', $field);
432-
$fieldKey = $fieldSplitArray[1];
432+
$fieldKey = $fieldSplitArray[1] ?? null;
433433

434434
if (is_array($fieldData)) {
435-
return ! empty(dot_array_search($otherField, $data)[$fieldKey]);
435+
if (empty($fieldData[$fieldKey])) {
436+
return false;
437+
}
438+
439+
continue;
436440
}
437-
$nowField = str_replace('*', $fieldKey, $otherField);
441+
442+
$nowField = str_replace('*', (string) $fieldKey, $otherField);
438443
$nowFieldVaule = dot_array_search($nowField, $data);
439444

440-
return null !== $nowFieldVaule;
445+
if ($nowFieldVaule === null) {
446+
return false;
447+
}
441448
}
442449
}
443450

tests/system/Validation/ValidationTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,6 +1854,51 @@ public function testRequireWithoutWithAsterisk(): void
18541854
);
18551855
}
18561856

1857+
/**
1858+
* Test that `required_without` checks all fields in dot-notation when there are multiple fields.
1859+
*/
1860+
public function testRequireWithoutMultipleWithAsterisk(): void
1861+
{
1862+
$data = [
1863+
'a' => [
1864+
['b' => 1, 'c' => 2, 'd' => ''],
1865+
['b' => 1, 'c' => '', 'd' => ''],
1866+
],
1867+
];
1868+
1869+
$this->validation->setRules([
1870+
'a.*.d' => 'required_without[a.*.b,a.*.c]',
1871+
])->run($data);
1872+
1873+
$this->assertSame(
1874+
'The a.*.d field is required when a.*.b,a.*.c is not present.',
1875+
$this->validation->getError('a.1.d'),
1876+
);
1877+
}
1878+
1879+
/**
1880+
* Test that `required_without` handles a non-asterisk field checked against an asterisk field
1881+
* without throwing undefined array key warnings for `$fieldSplitArray[1]`.
1882+
*/
1883+
public function testRequireWithoutAsteriskOnNonAsteriskField(): void
1884+
{
1885+
$data = [
1886+
'foo' => '',
1887+
'a' => [
1888+
['b' => ''],
1889+
],
1890+
];
1891+
1892+
$this->validation->setRules([
1893+
'foo' => 'required_without[a.*.b]',
1894+
])->run($data);
1895+
1896+
$this->assertSame(
1897+
'The foo field is required when a.*.b is not present.',
1898+
$this->validation->getError('foo'),
1899+
);
1900+
}
1901+
18571902
/**
18581903
* @see https://github.com/codeigniter4/CodeIgniter4/issues/8128
18591904
*/

user_guide_src/source/changelogs/v4.7.4.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Bugs Fixed
4141
- **Model:** Fixed a bug in ``Model::objectToRawArray()`` where the ``$recursive`` parameter was ignored.
4242
- **Session:** Fixed a bug in ``RedisHandler`` where the configured ``$lockMaxRetries`` and ``$lockRetryInterval`` values were not respected when acquiring session locks.
4343
- **Testing:** Fixed a bug where using ``MockInputOutput`` within a test that also uses ``StreamFilterTrait`` tore down the trait's stream filters, so CLI output produced after the ``MockInputOutput`` interaction (such as in ``tearDown()``) was no longer captured and leaked to the console.
44+
- **Validation:** Fixed bugs in the ``required_without`` rule logic where using array dot notation caused early exits ignoring subsequent fields and triggered an ``Undefined array key`` warning for missing keys.
4445

4546
See the repo's
4647
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

0 commit comments

Comments
 (0)