Skip to content
Closed
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
48 changes: 31 additions & 17 deletions system/Validation/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,30 +414,44 @@ public function required_without(
// Still here? Then we fail this test if
// any of the fields are not present in $data
foreach (explode(',', $otherFields) as $otherField) {
if (
(! str_contains($otherField, '.'))
&& (! array_key_exists($otherField, $data)
|| empty($data[$otherField]))
) {
return false;
if (! str_contains($otherField, '.')) {
if (! array_key_exists($otherField, $data) || empty($data[$otherField])) {
return false;
}

continue;
}

if (str_contains($otherField, '.')) {
if ($field === null) {
throw new InvalidArgumentException('You must supply the parameters: field.');
}
if ($field === null) {
throw new InvalidArgumentException('You must supply the parameters: field.');
}

$fieldData = dot_array_search($otherField, $data);
$fieldSplitArray = explode('.', $field);
$fieldKey = $fieldSplitArray[1];
$fieldSplitArray = explode('.', $field);
$fieldKey = $fieldSplitArray[1] ?? null;

if (is_array($fieldData)) {
return ! empty(dot_array_search($otherField, $data)[$fieldKey]);
if ($fieldKey === null) {
throw new InvalidArgumentException('Invalid field format for dot-path required_without.');
}

$fieldData = dot_array_search($otherField, $data);

if (is_array($fieldData)) {
$searched = dot_array_search($otherField, $data);

if (! is_array($searched) || ! array_key_exists($fieldKey, $searched)) {
return false;
}

if (empty($searched[$fieldKey])) {
return false;
}
} else {
$nowField = str_replace('*', $fieldKey, $otherField);
$nowFieldVaule = dot_array_search($nowField, $data);
$nowFieldValue = dot_array_search($nowField, $data);

return null !== $nowFieldVaule;
if ($nowFieldValue === null) {
return false;
}
}
}

Expand Down
40 changes: 40 additions & 0 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,46 @@ public function testRequireWithoutWithAsterisk(): void
);
}

public function testRequireWithoutWithMultipleAsterisk(): void
{
$data = [
'a' => [
['b' => 1, 'c' => 2, 'd' => 3],
['c' => '', 'd' => 4],
['b' => 5],
],
];

$this->validation->setRules([
'a.*.c' => 'required_without[a.*.b, a.*.d]',
])->run($data);

$this->assertSame(
'The a.*.c field is required when a.*.b, a.*.d is not present.',
$this->validation->getError('a.1.c'),
);
$this->assertArrayNotHasKey('a.0.c', $this->validation->getErrors(), 'Row 0: both b and d present');
$this->assertArrayHasKey('a.2.c', $this->validation->getErrors(), 'Row 2: c missing, d missing → field required');
}

public function testRequireWithoutWithMultipleAsteriskLastMissing(): void
{
$data = [
'a' => [
['b' => 1, 'c' => ''],
],
];

$this->validation->setRules([
'a.*.c' => 'required_without[a.*.b, a.*.nonexistent]',
])->run($data);

$this->assertSame(
'The a.*.c field is required when a.*.b, a.*.nonexistent is not present.',
$this->validation->getError('a.0.c'),
);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/8128
*/
Expand Down
Loading