Skip to content

Commit 2cd2f69

Browse files
committed
test: add unit tests for required_without with multiple dot-path fields
1 parent efdf83d commit 2cd2f69

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

‎tests/system/Validation/ValidationTest.php‎

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

1857+
public function testRequireWithoutWithMultipleAsterisk(): void
1858+
{
1859+
$data = [
1860+
'a' => [
1861+
['b' => 1, 'c' => 2, 'd' => 3],
1862+
['c' => '', 'd' => 4],
1863+
['b' => 5],
1864+
],
1865+
];
1866+
1867+
$this->validation->setRules([
1868+
'a.*.c' => 'required_without[a.*.b, a.*.d]',
1869+
])->run($data);
1870+
1871+
$this->assertSame(
1872+
'The a.*.c field is required when a.*.b, a.*.d is not present.',
1873+
$this->validation->getError('a.1.c'),
1874+
);
1875+
$this->assertArrayNotHasKey('a.0.c', $this->validation->getErrors(), 'Row 0: both b and d present');
1876+
$this->assertArrayHasKey('a.2.c', $this->validation->getErrors(), 'Row 2: c missing, d missing → field required');
1877+
}
1878+
1879+
public function testRequireWithoutWithMultipleAsteriskLastMissing(): void
1880+
{
1881+
$data = [
1882+
'a' => [
1883+
['b' => 1, 'c' => ''],
1884+
],
1885+
];
1886+
1887+
$this->validation->setRules([
1888+
'a.*.c' => 'required_without[a.*.b, a.*.nonexistent]',
1889+
])->run($data);
1890+
1891+
$this->assertSame(
1892+
'The a.*.c field is required when a.*.b, a.*.nonexistent is not present.',
1893+
$this->validation->getError('a.0.c'),
1894+
);
1895+
}
1896+
18571897
/**
18581898
* @see https://github.com/codeigniter4/CodeIgniter4/issues/8128
18591899
*/

0 commit comments

Comments
 (0)