From 2fa0227e90e6ab443f0cc84fa0ce34033fecc4ac 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 8484693a7623..3ea888875eca 100644 --- a/tests/system/Validation/ValidationTest.php +++ b/tests/system/Validation/ValidationTest.php @@ -1631,4 +1631,50 @@ public function testRequireWithoutWithWildCard(): void $this->validation->getError('a.1.c') ); } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/8128 + */ + public function testRuleWithMultiDimensionalArrayAndAsterisk(): 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() + ); + } }