4

I'm creating validation (via FormRequest ) for my API and I need to change status code depending on failed validation rule (e.g. If id is string instead of int, get 400. If id doesn't exists, get 404).

I wanted to write something like that:

/**
 * Get the proper failed validation response for the request.
 *
 * @param  array  $errors
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function response(array $errors)
{
    $failedRules = $this->getValidatorInstance()->failed();

    $statusCode = 400;
    if (isset($failedRules['id']['Exists'])) $statusCode = 404;

    return response($errors, $statusCode);
}

However, $this->getValidatorInstance()->failed() returns empty array

  • Why does $this->getValidatorInstance()->failed return empty array?
  • How can I fix that? Is there some other way to return status code depending on failed validation rule?
2
  • What version of laravel are you using? Commented Jan 4, 2017 at 18:30
  • I use Laravel 5.3 Commented Jan 4, 2017 at 18:46

1 Answer 1

3

The reason you're getting an empty array when your call $this->getValidatorInstance()->failed() is because it's actually resolving a new instance of Validator.

What you can do is call passes() on the new Validator instance which will then allow you to call failed() to get the rules:

$validator = $this->getValidatorInstance();
$validator->passes();
$failedRules = $validator->failed();

Alternatively, if you don't want to have the validator run twice you could override the failedValidation method to store the Validation instance in class:

protected $currentValidator;

protected function failedValidation(Validator $validator)
{
    $this->currentValidator = $validator;

    throw new ValidationException($validator, $this->response(
        $this->formatErrors($validator)
    ));
}

public function response(array $errors)
{
    $failedRules = $this->currentValidator->failed();

    $statusCode = 400;
    if (isset($failedRules['id']['Exists'])) $statusCode = 404;

    return response($errors, $statusCode);
}

Hope this helps!

Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, I can do that, but in this case I validate data twice so it not helps me
I can't do that, bcs I get "Declaration of ...GetUser::response(array $errors, $validator) should be compatible with ...FormRequest::response(array $errors)" At least, i'm gonna to create my own class. Can u tell me, how i should do that? I mean, where should i create it? I thought i can create Facade, but i can't do that or docs does not say anything about that :)
@Ivan Oh yeah, of course it does. In that case you could add it to a property in your FormRequest and then reference it after. I've update my answer.
Thanks for help. Nevermind, I'm gonna create new class, bcs I have to create twentyish classes like that :)
@Ivan Glad I could help! Just create one class with the above logic and have your twentyish classes extend it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.