From 09e194dc591633dbac0aad96a3987ff671e4c3f4 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 31 Oct 2023 09:43:51 +0900 Subject: [PATCH] test: add test for multidimensinal array and `*` --- tests/system/Validation/ValidationTest.php | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/system/Validation/ValidationTest.php b/tests/system/Validation/ValidationTest.php index 7f11b86d916a..7cc6f99c40d5 100644 --- a/tests/system/Validation/ValidationTest.php +++ b/tests/system/Validation/ValidationTest.php @@ -1631,4 +1631,50 @@ public function testRequireWithoutWithAsterisk(): void $this->validation->getError('a.1.c') ); } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/8128 + */ + public function testRuleWithAsteriskToMultiDimensionalArray(): void + { + $data = [ + 'contacts' => [ + 'name' => 'Joe Smith', + 'just' => [ + 'friends' => [ + [ + 'name' => 'Fred Flinstone', + ], + [ + 'name' => 'Wilma', + ], + ], + ], + ], + ]; + + $this->validation->setRules( + ['contacts.just.friends.*.name' => 'required|max_length[1]'] + ); + $this->assertFalse($this->validation->run($data)); + $this->assertSame( + [ + 'contacts.just.friends.0.name' => 'The contacts.just.friends.*.name field cannot exceed 1 characters in length.', + 'contacts.just.friends.1.name' => 'The contacts.just.friends.*.name field cannot exceed 1 characters in length.', + ], + $this->validation->getErrors() + ); + + $this->validation->reset(); + $this->validation->setRules( + ['contacts.*.name' => 'required|max_length[1]'] + ); + $this->assertFalse($this->validation->run($data)); + $this->assertSame( + // The data for `contacts.*.name` does not exist. So it is interpreted + // as `null`, and this error message returns. + ['contacts.*.name' => 'The contacts.*.name field is required.'], + $this->validation->getErrors() + ); + } }