【发布时间】:2017-01-05 04:28:42
【问题描述】:
我正在为我的 API 创建验证(通过 FormRequest ),我需要根据失败的验证规则更改状态代码(例如,如果 id 是字符串而不是 int,则获取 400。如果 id 不存在,获取 404) .
我想写这样的东西:
/**
* 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);
}
但是,$this->getValidatorInstance()->failed() 返回空数组
- 为什么 $this->getValidatorInstance()->failed 返回空数组?
- 我该如何解决这个问题?是否有其他方法可以根据失败的验证规则返回状态码?
【问题讨论】:
-
你用的是什么版本的 laravel?
-
我使用 Laravel 5.3