在laravel 5.2中,我能够覆盖单个表单请求的响应,如下所示,并使用dontFlash
属性排除闪烁的某些字段:
public function response(array $errors)
{
// action is a hidden form input
$this->redirect = $this->getRedirectUrl() . '#' . $this->action;
$this->errorBag = $this->action;
$this->dontFlash[] = 'action';
if ($this->ajax() || $this->wantsJson()) {
return new JsonResponse($errors, 422);
}
return $this->redirector->to($this->getRedirectUrl())
->withInput($this->except($this->dontFlash))
->withErrors($errors, $this->errorBag);
}
在laravel 5.5中,dontflash
属性已被删除,我们建议覆盖failedvalidation
方法,如下所示:
protected function failedValidation(Validator $validator)
{
$this->redirect = $this->getRedirectUrl() . '#' . $this->action;
$this->errorBag = $this->action;
$this->dontFlash[] = 'action';
throw (new ValidationException($validator))
->errorBag($this->errorBag)
->redirectTo($this->getRedirectUrl());
}
所以我的问题是如何排除闪烁某些字段 - 上面的抛出错误:
Indirect modification of overloaded property
App\Http\Requests\MyFormRequest::$dontFlash has no effect
我能想到的唯一方法是将字段名称添加到dontFlash
文件中的exceptions\handler.php
属性
但是它会对包含具有该名称的字段的每个表单生效,这不是我想要的行为。
我是如何实现这一目标的?