Skip to content

Bug: ValidationRules::buildUserFromRequest() does not support json input #612

@ValdisM

Description

@ValdisM

PHP Version

8

CodeIgniter4 Version

4.3.0

Shield Version

dev-develop

Which operating systems have you tested for this bug?

macOS

Which server did you use?

apache

Database

MariaDB

Did you customize Shield?

   protected function buildUserFromRequest(): User
    {
        $fields = $this->prepareValidFields();
        $request = service('request');
        $data = $request->getJSON() ? (array) $request->getJSON() : $request->getPost($fields);
        return new User($data);
    }

What happened?

buildUserFromRequest support only form data inputs, not json input from service("request").
Before:

$data = $request->getPost($fields);

After:

$data = $request->getJSON() ? (array) $request->getJSON() : $request->getPost($fields);

Steps to Reproduce

$this->validation->setRules([
			"email" => ["label" => "Email", "rules" => "required|max_length[254]|valid_email"],
			"newPassword" => ["label" => "New password", "rules" => "required|strong_password"],
			"newPasswordAgain" => ["label" => "Repeat password", "rules" => "required|matches[newPassword]"],
		]);

it was catched when "strong_password" rule was applied.

Expected Output

/**
     * Builds a new user instance from the global request.
     */
    protected function buildUserFromRequest(): User
    {
        $fields = $this->prepareValidFields();

        /** @var IncomingRequest $request */
        $request = service('request');

        $data = $request->getJSON() ? (array) $request->getJSON() : $request->getPost($fields);

        return new User($data);
    }

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingwaiting for infoIssues or pull requests that need further clarification from the author

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions