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

Bug: return $this->failValidationError($validation->getErrors()) Has Invalid Signature #4506

Closed
adamfisher opened this issue Mar 30, 2021 · 1 comment · Fixed by #4546
Closed
Labels
documentation Pull requests for documentation only

Comments

@adamfisher
Copy link

adamfisher commented Mar 30, 2021

The documentation for return $this->failValidationError($validation->getErrors()); shows this line is working in the documentation but in reality, I keep getting a 500 error. I am trying to build some validation for my REST API.

But if I pass the following manually then the list of validation errors returns successfully:

return $this->fail($this->validator->getErrors());

CodeIgniter 4 version
4.1.1

Affected module(s)
trait ResponseTrait

Expected behavior, and steps to reproduce if appropriate
Create a POST method in a controller like this and it will fail with 500:

    public function create()
    {
        if($this->validate('vendor')) {
            $vendor = $this->request->getJSON();
            $this->model->insert($vendor);
            return $this->respondCreated();
        }
        return $this->failValidationError($this->validator->getErrors());
    }

but change the last line to this:

return $this->fail($this->validator->getErrors());

and you successfully get back:

{
    "status": 400,
    "error": 400,
    "messages": {
        "first_name": "The first_name field may only contain alphabetical characters.",
        "last_name": "The last_name field may only contain alphabetical characters.",
        "account_type": "The account_type field must be one of: checking,savings.",
        "signature_date": "The signature_date field must contain a valid date."
    }
}

Context
Docker Image: webdevops/php-apache-dev:7.4

@adamfisher adamfisher added the bug Verified issues on the current code behavior or pull requests that will fix them label Mar 30, 2021
@kenjis
Copy link
Member

kenjis commented Apr 3, 2021

It seems the sample code is not correct.

public function testValidationError()
{
$controller = $this->makeController();
$controller->failValidationError('Nope', 'FAT CHANCE', 'A Custom Reason');
$expected = [
'status' => 400,
'error' => 'FAT CHANCE',
'messages' => [
'error' => 'Nope',
],
];
$this->assertEquals('A Custom Reason', $this->response->getReason());
$this->assertEquals(400, $this->response->getStatusCode());
$this->assertEquals($this->formatter->format($expected), $this->response->getBody());
}

@paulbalandan paulbalandan added documentation Pull requests for documentation only and removed bug Verified issues on the current code behavior or pull requests that will fix them labels Apr 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Pull requests for documentation only
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants