Skip to content
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

feat: add param $withSessionErrors to Validation::getErrors() #6381

Closed
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 @@ -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
21 changes: 16 additions & 5 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -675,20 +675,31 @@ 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'])) {
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']) && (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
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Others
- The ``spark`` file has been changed due to a change in the processing of Spark commands.
- ``InvalidArgumentException`` that is a kind of ``LogicException`` in ``BaseBuilder::_whereIn()`` is not suppressed by the configuration. Previously if ``CI_DEBUG`` was false, the exception was suppressed.
- ``RouteCollection::resetRoutes()`` resets Auto-Discovery of Routes. Previously once discovered, RouteCollection never discover Routes files again even if ``RouteCollection::resetRoutes()`` is called.
- The method signature of ``Validation::getErrors()`` has been changed. A first optional parameter ``$withSessionErrors`` was added. When you pass ``false``, the method does not return errors that are stored in the session.

Enhancements
************
Expand Down