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?