Skip to content

Commit

Permalink
Merge pull request #9142 from kenjis/fix-validation-numeric-field-name
Browse files Browse the repository at this point in the history
fix: [Validation] TypeError when using numeric field names
  • Loading branch information
kenjis authored Aug 27, 2024
2 parents 219010e + 2979ece commit 82584a6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
6 changes: 0 additions & 6 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -10459,12 +10459,6 @@
'count' => 1,
'path' => __DIR__ . '/system/Validation/Validation.php',
];
$ignoreErrors[] = [
// identifier: missingType.iterableValue
'message' => '#^Property CodeIgniter\\\\Validation\\\\Validation\\:\\:\\$rules type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Validation/Validation.php',
];
$ignoreErrors[] = [
// identifier: missingType.iterableValue
'message' => '#^Property CodeIgniter\\\\Validation\\\\Validation\\:\\:\\$validated type has no value type specified in iterable type array\\.$#',
Expand Down
5 changes: 4 additions & 1 deletion system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Validation implements ValidationInterface
/**
* Stores the actual rules that should be run against $data.
*
* @var array
* @var array<array-key, array{label?: string, rules: list<string>}>
*
* [
* field1 => [
Expand Down Expand Up @@ -163,6 +163,9 @@ public function run(?array $data = null, ?string $group = null, $dbGroup = null)
// Run through each rule. If we have any field set for
// this rule, then we need to run them through!
foreach ($this->rules as $field => $setup) {
// An array key might be int.
$field = (string) $field;

$rules = $setup['rules'];

if (is_string($rules)) {
Expand Down
10 changes: 10 additions & 0 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,16 @@ public function testRunDoesTheBasics(): void
$this->assertSame([], $this->validation->getValidated());
}

public function testRunWithNumericFieldName(): void
{
// The array key will be int.
$data = ['123' => 'notanumber'];
$this->validation->setRules(['123' => 'is_numeric']);

$this->assertFalse($this->validation->run($data));
$this->assertSame([], $this->validation->getValidated());
}

public function testClosureRule(): void
{
$this->validation->setRules(
Expand Down

0 comments on commit 82584a6

Please sign in to comment.