Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions system/Validation/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading