From 33477f8d4e67f4f5121dabe331bb2577da315e4d Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 12 Jun 2024 10:27:28 +0900 Subject: [PATCH 1/3] test: add test for if_exist and array data --- tests/system/Validation/RulesTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/system/Validation/RulesTest.php b/tests/system/Validation/RulesTest.php index 25b3510323bb..a2556085e559 100644 --- a/tests/system/Validation/RulesTest.php +++ b/tests/system/Validation/RulesTest.php @@ -100,6 +100,12 @@ public static function provideIfExist(): iterable ['foo' => ''], false, ], + // Invalid array input + [ + ['foo' => 'if_exist|alpha'], + ['foo' => ['bar' => '12345']], + false, + ], // Input data does not exist then the other rules will be ignored [ ['foo' => 'if_exist|required'], From 1ca24da3d4f439d48eb658ef1fd9b6f3452375b0 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 12 Jun 2024 10:28:10 +0900 Subject: [PATCH 2/3] fix: if_exist does not work with array data --- system/Validation/FormatRules.php | 4 ++++ system/Validation/Validation.php | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/system/Validation/FormatRules.php b/system/Validation/FormatRules.php index 7fe16d9d2772..fc9fd73ed80d 100644 --- a/system/Validation/FormatRules.php +++ b/system/Validation/FormatRules.php @@ -29,6 +29,10 @@ class FormatRules */ public function alpha($str = null): bool { + if (is_array($str)) { + return false; + } + if (! is_string($str)) { $str = (string) $str; } diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index eb87ef3fa776..bfc661c2b3d4 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -399,8 +399,10 @@ private function processIfExist(string $field, array $rules, array $data) break; } } - } else { + } elseif (str_contains($field, '.')) { $dataIsExisting = array_key_exists($ifExistField, $flattenedData); + } else { + $dataIsExisting = array_key_exists($ifExistField, $data); } if (! $dataIsExisting) { From cdec7d0b39f06a58773098a70da63cfaef7d9e84 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 13 Jun 2024 08:04:39 +0900 Subject: [PATCH 3/3] fix: revert rule `alpha` --- system/Validation/FormatRules.php | 4 ---- tests/system/Validation/RulesTest.php | 20 +++++++++++++------ .../Validation/StrictRules/RulesTest.php | 10 ++++++++++ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/system/Validation/FormatRules.php b/system/Validation/FormatRules.php index fc9fd73ed80d..7fe16d9d2772 100644 --- a/system/Validation/FormatRules.php +++ b/system/Validation/FormatRules.php @@ -29,10 +29,6 @@ class FormatRules */ public function alpha($str = null): bool { - if (is_array($str)) { - return false; - } - if (! is_string($str)) { $str = (string) $str; } diff --git a/tests/system/Validation/RulesTest.php b/tests/system/Validation/RulesTest.php index a2556085e559..7c69fa24838e 100644 --- a/tests/system/Validation/RulesTest.php +++ b/tests/system/Validation/RulesTest.php @@ -15,6 +15,7 @@ use CodeIgniter\Test\CIUnitTestCase; use Config\Services; +use ErrorException; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use stdClass; @@ -100,12 +101,6 @@ public static function provideIfExist(): iterable ['foo' => ''], false, ], - // Invalid array input - [ - ['foo' => 'if_exist|alpha'], - ['foo' => ['bar' => '12345']], - false, - ], // Input data does not exist then the other rules will be ignored [ ['foo' => 'if_exist|required'], @@ -132,6 +127,19 @@ public static function provideIfExist(): iterable ]; } + public function testIfExistArray(): void + { + $this->expectException(ErrorException::class); + $this->expectExceptionMessage('Array to string conversion'); + + $rules = ['foo' => 'if_exist|alpha']; + // Invalid array input + $data = ['foo' => ['bar' => '12345']]; + + $this->validation->setRules($rules); + $this->validation->run($data); + } + #[DataProvider('providePermitEmpty')] public function testPermitEmpty(array $rules, array $data, bool $expected): void { diff --git a/tests/system/Validation/StrictRules/RulesTest.php b/tests/system/Validation/StrictRules/RulesTest.php index e211ed38e888..7589360d0efe 100644 --- a/tests/system/Validation/StrictRules/RulesTest.php +++ b/tests/system/Validation/StrictRules/RulesTest.php @@ -244,4 +244,14 @@ public static function provideDiffers(): iterable 'foo bar bool match' => [['foo' => true, 'bar' => true], false], ]; } + + public function testIfExistArray(): void + { + $rules = ['foo' => 'if_exist|alpha']; + // Invalid array input + $data = ['foo' => ['bar' => '12345']]; + + $this->validation->setRules($rules); + $this->assertFalse($this->validation->run($data)); + } }