Skip to content

Commit

Permalink
Merge pull request #5861 from kenjis/fix-model-validation-twice
Browse files Browse the repository at this point in the history
fix: validation errors in Model are not cleared when running validation again
  • Loading branch information
kenjis authored Apr 6, 2022
2 parents 55d64e9 + e4f1d88 commit d4d2e97
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ parameters:
count: 1
path: system/Autoloader/Autoloader.php

-
message: "#^Method CodeIgniter\\\\Validation\\\\ValidationInterface\\:\\:run\\(\\) invoked with 3 parameters, 0\\-2 required\\.$#"
count: 1
path: system/BaseModel.php

-
message: "#^Property Config\\\\Cache\\:\\:\\$backupHandler \\(string\\) in isset\\(\\) is not nullable\\.$#"
count: 1
Expand Down
4 changes: 3 additions & 1 deletion system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,9 @@ public function validate($data): bool
return true;
}

return $this->validation->setRules($rules, $this->validationMessages)->run($data, null, $this->DBGroup);
$this->validation->reset()->setRules($rules, $this->validationMessages);

return $this->validation->run($data, null, $this->DBGroup);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/system/Models/ValidationModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ public function testValidationBasics(): void
$this->assertSame('You forgot to name the baby.', $errors['name']);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5859
*/
public function testValidationTwice(): void
{
$data = [
'name' => null,
'description' => 'some great marketing stuff',
];

$this->assertFalse($this->model->insert($data));

$errors = $this->model->errors();
$this->assertSame('You forgot to name the baby.', $errors['name']);

$data = [
'name' => 'some name',
'description' => 'some great marketing stuff',
];

$this->assertIsInt($this->model->insert($data));
}

public function testValidationWithSetValidationRule(): void
{
$data = [
Expand Down

0 comments on commit d4d2e97

Please sign in to comment.