Skip to content

Bug: Docs/API type mismatch for ResponseTrait::failValidationErrors() vs Validation::getErrors(); user guide example is misleading #9741

@campio97

Description

@campio97

PHP Version

8.3

CodeIgniter4 Version

4.6.3

CodeIgniter4 Installation Method

Composer (as dependency to an existing project)

Which operating systems have you tested for this bug?

macOS

Which server did you use?

cli-server (PHP built-in webserver)

Database

MySQL 8

What happened?

The user guide shows calling failValidationErrors($validation->getErrors()) directly, which naturally suggests passing $this->validator->getErrors() from the Validation library. However, Validation::getErrors() returns array<string, string> (field => message), while ResponseTrait::failValidationErrors() is typed to accept array<int, string>|string (a list of messages or a single string). This causes static analysis errors when following the docs literally.

Steps to Reproduce

use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Controller;

class Users extends Controller
{
    use ResponseTrait;

    public function create()
    {
        $rules = ['email' => 'required|valid_email'];

        if (! $this->validate($rules)) {
            // From docs pattern
            return $this->failValidationErrors($this->validator->getErrors());
        }

        return $this->respondCreated();
    }
}

Run PHPStan on the controller. It reports:
“Parameter #1 $errors of method …::failValidationErrors() expects list<string>|string, array<string, string> given.”

Actual
Static analysis fails when passing the associative array returned by Validation::getErrors() to failValidationErrors(). The method’s phpdoc/API signature requires array<int, string>|string, i.e., a list.

Expected Output

Following the user guide example should type-check cleanly without additional transformations.

  1. Update ResponseTrait::failValidationErrors() parameter type to accept associative arrays as well, e.g. array<array-key, string>|string (or array<int|string, string>|string).
  2. Update the user guide example to pass a list of messages, e.g. array_values($this->validator->getErrors()), and explicitly note the expected shape.
  3. Align the API reference and phpdoc with whichever approach is chosen so that docs and types match.

Anything else?

References:

  1. User guide showing failValidationErrors($errors) usage
  2. API reference for ResponseTrait::failValidationErrors() indicating array<int, string>|string
  3. API reference for Validation::getErrors() returning array<string, string>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions