当表单验证失败时,我的请求对象没有接收到 old() 数据。我收到了错误消息,但没有收到任何旧的输入数据。
我已经阅读了一些关于类似问题的解决方案,这些解决方案引用了对 Controller 上的重定向进行更改,但这并不能解决这个问题,因为重定向是由 FormRequest 类中下面引用的函数而不是 Controller 完成的.
有没有人遇到过同样的问题?当我阅读一些引用现有错误的论坛时,我已经升级了我的实例,但问题仍然存在。
如有任何帮助,我们将不胜感激。
版本: 我已经在 laravel 5.4、5.7 和 5.8 上试过了,它们都没有渲染旧数据。
我如何进行请求验证 验证是通过扩展 FormRequest 的标准请求文件完成的。
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CustomerManagementRequest extends FormRequest {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize() {
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules() {
return [
'first_name' => 'required|min:2',
'last_name' => 'required|min:2',
'email' => 'required|min:4',
];
}
}
我如何尝试访问我 View 中的旧数据:
value="{{old('first_name')}}"
FormRequest 中的重定向和验证 重定向是通过 Laravel 标准 FormRequest 类完成的。
protected function failedValidation(Validator $validator)
{
throw (new ValidationException($validator))
->errorBag($this->errorBag)
->redirectTo($this->getRedirectUrl());
}
验证器响应: 这是我在 vardump 上面的函数时看到的。它确实有我的表单数据。
Validator {#629 ▼
#data: array:11 [▼
"_token" => "1ynKXxi551UBGbJq6ftLsW6VsClZzmbZdHiHIxyt"
"active_from_date" => "04/04/2019 10:58 PM"
"last_sync_date" => "04/04/2019 11:00 PM"
"first_name" => "Pizza"
"last_name" => "Dough"
"email" => null
"full_phone" => null
"phone" => null
]
}
在 View 中转储 old() 参数返回一个空数组 []
最佳答案
验证后,您应该检查所有验证是否通过。在 Controller 的方法中你可以这样做,
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
$messages = $validator->errors()->getMessages();
return back()
->withErrors($messages)
->withInput();
}
希望这有帮助:)
关于php - 验证失败后 FormRequest 不填充 $request->old(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55516324/