Skip to content

Commit d4d2e97

Browse files
authored
Merge pull request #5861 from kenjis/fix-model-validation-twice
fix: validation errors in Model are not cleared when running validation again
2 parents 55d64e9 + e4f1d88 commit d4d2e97

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

phpstan-baseline.neon.dist

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ parameters:
2525
count: 1
2626
path: system/Autoloader/Autoloader.php
2727

28-
-
29-
message: "#^Method CodeIgniter\\\\Validation\\\\ValidationInterface\\:\\:run\\(\\) invoked with 3 parameters, 0\\-2 required\\.$#"
30-
count: 1
31-
path: system/BaseModel.php
32-
3328
-
3429
message: "#^Property Config\\\\Cache\\:\\:\\$backupHandler \\(string\\) in isset\\(\\) is not nullable\\.$#"
3530
count: 1

system/BaseModel.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,9 @@ public function validate($data): bool
13441344
return true;
13451345
}
13461346

1347-
return $this->validation->setRules($rules, $this->validationMessages)->run($data, null, $this->DBGroup);
1347+
$this->validation->reset()->setRules($rules, $this->validationMessages);
1348+
1349+
return $this->validation->run($data, null, $this->DBGroup);
13481350
}
13491351

13501352
/**

tests/system/Models/ValidationModelTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@ public function testValidationBasics(): void
5555
$this->assertSame('You forgot to name the baby.', $errors['name']);
5656
}
5757

58+
/**
59+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5859
60+
*/
61+
public function testValidationTwice(): void
62+
{
63+
$data = [
64+
'name' => null,
65+
'description' => 'some great marketing stuff',
66+
];
67+
68+
$this->assertFalse($this->model->insert($data));
69+
70+
$errors = $this->model->errors();
71+
$this->assertSame('You forgot to name the baby.', $errors['name']);
72+
73+
$data = [
74+
'name' => 'some name',
75+
'description' => 'some great marketing stuff',
76+
];
77+
78+
$this->assertIsInt($this->model->insert($data));
79+
}
80+
5881
public function testValidationWithSetValidationRule(): void
5982
{
6083
$data = [

0 commit comments

Comments
 (0)