Skip to content

fix: validation errors in Model are not cleared when running validation again #5861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Comment on lines +1347 to +1349
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside from adding the ->reset() method, may I know why the line got broken into two? Or was this because of line length?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is too long.
And one line is a bit difficult to debug, step debugging.

}

/**
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