Skip to content

Commit

Permalink
feat: add param $withSessionErrors to Validation::getErrors()
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Aug 16, 2022
1 parent e6f167f commit 077cf51
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -650,11 +650,6 @@ parameters:
count: 1
path: system/Throttle/Throttler.php

-
message: "#^Property CodeIgniter\\\\Validation\\\\Validation\\:\\:\\$errors \\(array\\) on left side of \\?\\? is not nullable\\.$#"
count: 1
path: system/Validation/Validation.php

-
message: "#^Variable \\$error on left side of \\?\\? always exists and is always null\\.$#"
count: 1
Expand Down
25 changes: 19 additions & 6 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -675,20 +675,33 @@ public function getError(?string $field = null): string
* 'field2' => 'error message',
* ]
*
* @param bool $withSessionErrors Whether to get the validation errors in Session by redirect()->withErrors().
*
* @return array<string, string>
*
* @codeCoverageIgnore
*/
public function getErrors(): array
public function getErrors(bool $withSessionErrors = true): array
{
// If we already have errors, we'll use those.
// If we don't, check the session to see if any were
// passed along from a redirect_with_input request.
if (empty($this->errors) && ! is_cli() && isset($_SESSION, $_SESSION['_ci_validation_errors'])) {
$this->errors = unserialize($_SESSION['_ci_validation_errors']);
if ($this->errors !== []) {
return $this->errors;
}

// If we don't get the errors in the Session.
if (! $withSessionErrors) {
return $this->errors;
}

// Check the session to see if any were
// passed along from a redirect withErrors() request.
if (isset($_SESSION, $_SESSION['_ci_validation_errors'])) {
if (ENVIRONMENT === 'testing' || ! is_cli()) {
$this->errors = unserialize($_SESSION['_ci_validation_errors']);
}
}

return $this->errors ?? [];
return $this->errors;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,24 @@ public function testGetErrors(): void
$this->assertSame(['foo' => 'Validation.is_numeric'], $this->validation->getErrors());
}

public function testGetErrorsWithSession(): void
{
$_SESSION = ['_ci_validation_errors' => 'a:1:{s:3:"foo";s:3:"bar";}'];

$this->assertSame(['foo' => 'bar'], $this->validation->getErrors());

$_SESSION = [];
}

public function testGetErrorsWithoutSessionErrorDataWithSession(): void
{
$_SESSION = ['_ci_validation_errors' => 'a:1:{s:3:"foo";s:3:"bar";}'];

$this->assertSame([], $this->validation->getErrors(false));

$_SESSION = [];
}

public function testGetErrorsWhenNone(): void
{
$data = ['foo' => 123];
Expand Down

0 comments on commit 077cf51

Please sign in to comment.