Repository: codeigniter4/CodeIgniter4 (PHP)

Issue:
Bug: [Validation] if_exist is not working as intended with arrays
### PHP Version

8.2

### CodeIgniter4 Version

4.4.8

### CodeIgniter4 Installation Method

Composer (using `codeigniter4/appstarter`)

### Which operating systems have you tested for this bug?

Linux

### Which server did you use?

apache

### Database

_No response_

### What happened?

Ok so based on the official CodeIgniter 4's documentation, the `if_exist` validation rule is supposed to prevent further validation when the field is not present in the data to validate.

However, I noticed that when sending an array it causes the `if_exist` to misbehave.

### Steps to Reproduce

To reproduce you can use this bit of code:
```PHP
$validation_rules = [
    'foo' => [
        'rules' => 'if_exist|in_list',
        'errors' => [
            'in_list' => 'invalid_parameter'
        ]
     ]
 ];
 $this->validate($validation_rules);
```

and send something like this in the payload (This is from Postman's Body using form-data):
```
foo[0]:bar
```

### Expected Output

The `validate()` function should return `false`

### Anything else?

_No response_


Discussion:
Check #8959



Patch:
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) {


Test patch:
diff --git a/tests/system/Validation/RulesTest.php b/tests/system/Validation/RulesTest.php
index 25b3510323bb..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;
@@ -126,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));
+    }
 }