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

fix: Call to a member function getErrors() on null in CheckQueryReturnTrait #1088

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Apr 8, 2024

Description
Fixes #1087

Since CI 4.5.0, $this->validation may be null.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

Copy link
Collaborator

@datamweb datamweb left a comment

Choose a reason for hiding this comment

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

@kenjis How about this?

    protected function checkValidationError(): void
    {
        $validationErrors = $this->validation?->getErrors();

        if ($validationErrors !== [] && $validationErrors !== null) {
            $message = 'Validation error:';

            foreach ($validationErrors as $field => $error) {
                $message .= ' [' . $field . '] ' . $error;
            }

            throw new ValidationException($message);
        }
    }

@kenjis kenjis force-pushed the fix-CheckQueryReturnTrait-validation-error-CI45 branch from 01d781b to 27a017d Compare April 8, 2024 11:22
@kenjis
Copy link
Member Author

kenjis commented Apr 8, 2024

@datamweb Ah, if $this->validation is null, the model did not run validation.
So, your way is the way to go.

@kenjis
Copy link
Member Author

kenjis commented Apr 8, 2024

$this->validation?->getErrors() requires PHP 8.0.
Shield still supports PHP 7.4.

"php": "^7.4.3 || ^8.0",

@kenjis kenjis force-pushed the fix-CheckQueryReturnTrait-validation-error-CI45 branch from bb27508 to 35bbb77 Compare April 8, 2024 12:25
Copy link
Collaborator

@datamweb datamweb left a comment

Choose a reason for hiding this comment

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

Thanks, the code is more readable.

@kenjis kenjis merged commit cf646b8 into codeigniter4:develop Apr 8, 2024
19 of 31 checks passed
@kenjis kenjis deleted the fix-CheckQueryReturnTrait-validation-error-CI45 branch April 8, 2024 12:35
@datamweb
Copy link
Collaborator

Wait for a new version of Shield to be released, or temporarily use the develop branch:

composer require codeigniter4/shield:dev-develop

@quickdunk
Copy link

Thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [CI 4.5.0] After registration/login, "Error: Call to a member function getErrors() on null"
3 participants