From 26714450ff97fe16130f127c54bd7f8db0581726 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Fri, 19 Jun 2026 21:31:54 +0200 Subject: [PATCH] fix(Validation): correct required_without logic and prevent array key warnings --- system/Validation/Rules.php | 15 +++++++++++---- tests/system/Validation/ValidationTest.php | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/system/Validation/Rules.php b/system/Validation/Rules.php index e3e256b099b4..69bc6e5ea2e6 100644 --- a/system/Validation/Rules.php +++ b/system/Validation/Rules.php @@ -429,15 +429,22 @@ public function required_without( $fieldData = dot_array_search($otherField, $data); $fieldSplitArray = explode('.', $field); - $fieldKey = $fieldSplitArray[1]; + $fieldKey = $fieldSplitArray[1] ?? null; if (is_array($fieldData)) { - return ! empty(dot_array_search($otherField, $data)[$fieldKey]); + if (empty($fieldData[$fieldKey])) { + return false; + } + + continue; } - $nowField = str_replace('*', $fieldKey, $otherField); + + $nowField = str_replace('*', (string) $fieldKey, $otherField); $nowFieldVaule = dot_array_search($nowField, $data); - return null !== $nowFieldVaule; + if (null === $nowFieldVaule) { + return false; + } } } diff --git a/tests/system/Validation/ValidationTest.php b/tests/system/Validation/ValidationTest.php index 7e0f4411f079..48c92aea3968 100644 --- a/tests/system/Validation/ValidationTest.php +++ b/tests/system/Validation/ValidationTest.php @@ -1854,6 +1854,28 @@ public function testRequireWithoutWithAsterisk(): void ); } + /** + * Test that `required_without` checks all fields in dot-notation when there are multiple fields. + */ + public function testRequireWithoutMultipleWithAsterisk(): void + { + $data = [ + 'a' => [ + ['b' => 1, 'c' => 2, 'd' => ''], + ['b' => 1, 'c' => '', 'd' => ''], + ], + ]; + + $this->validation->setRules([ + 'a.*.d' => 'required_without[a.*.b,a.*.c]', + ])->run($data); + + $this->assertSame( + 'The a.*.d field is required when a.*.b,a.*.c is not present.', + $this->validation->getError('a.1.d'), + ); + } + /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/8128 */