使用 FormResquest 时验证失败时的 Flash 错误消息
Flash Error message when Validation Fails while using FormResquest
我正在使用 FormRequest 进行验证。我正在尝试通过 Flash 设置顶级错误消息,以向用户显示表单提交未成功。
现在我有以下 UserResquest
class UserRequest 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|string|min:2|max:50',
'last_name' => 'required|string|min:2|max:50',
'date_of_birth' => 'required|date',
'gender' => 'required|in:male,female'
];
}
我正尝试在我的控制器中执行此操作
$validator = $request->validated();
if ($validator->fails()) {
Session::flash('error', $validator->messages()->first());
return redirect()->back()->withInput();
}
但是 Flash 消息显示为空。 使用 FormRequest
时设置 Flash 错误消息的最佳方法是什么
正在按照视图模板设置我的 Flash 消息。
<div class="flash-message">
@foreach (['danger', 'warning', 'success', 'info'] as $msg)
@if(Session::has('alert-' . $msg))
<p class="alert alert-{{ $msg }}">{{ Session::get('alert-' . $msg) }} <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a></p>
@endif
@endforeach
</div> <!-- end .flash-message -->
FormRequest
自动完成,您不必在控制器中处理它。
它在
protected function failedValidation(Validator $validator)
{
throw (new ValidationException($validator))
->errorBag($this->errorBag)
->redirectTo($this->getRedirectUrl());
}
如果您需要其他行为,可以重载该方法。
要获取错误消息,您需要在错误包中获取它
{{ $errors->default->first('first_name') }}
错误包默认命名为default
,您可以在FormRequest
扩展中更改它class
自定义消息
要设置每个错误的消息,请在 UserRequest
class
中声明以下方法
public function messages()
{
return [
'first_name.required' => 'A first name is required',
'first_name.min' => 'The first name can\'t be a single character',
...
];
}
要知道是否有错误,请检查 blade 中的变量 $errors->default
然后您可以显示消息“表单未保存。检查下面的错误并重试”
正如 N69S 指出的那样。我们可以在failedValidation
下设置如下。
/**
* Handle a failed validation attempt.
*/
protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
{
session()->flash('alert-danger', 'There was an error, Please try again!');
return parent::failedValidation($validator);
}
我正在使用 FormRequest 进行验证。我正在尝试通过 Flash 设置顶级错误消息,以向用户显示表单提交未成功。
现在我有以下 UserResquest
class UserRequest 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|string|min:2|max:50',
'last_name' => 'required|string|min:2|max:50',
'date_of_birth' => 'required|date',
'gender' => 'required|in:male,female'
];
}
我正尝试在我的控制器中执行此操作
$validator = $request->validated();
if ($validator->fails()) {
Session::flash('error', $validator->messages()->first());
return redirect()->back()->withInput();
}
但是 Flash 消息显示为空。 使用 FormRequest
正在按照视图模板设置我的 Flash 消息。
<div class="flash-message">
@foreach (['danger', 'warning', 'success', 'info'] as $msg)
@if(Session::has('alert-' . $msg))
<p class="alert alert-{{ $msg }}">{{ Session::get('alert-' . $msg) }} <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a></p>
@endif
@endforeach
</div> <!-- end .flash-message -->
FormRequest
自动完成,您不必在控制器中处理它。
它在
protected function failedValidation(Validator $validator)
{
throw (new ValidationException($validator))
->errorBag($this->errorBag)
->redirectTo($this->getRedirectUrl());
}
如果您需要其他行为,可以重载该方法。
要获取错误消息,您需要在错误包中获取它
{{ $errors->default->first('first_name') }}
错误包默认命名为default
,您可以在FormRequest
扩展中更改它class
自定义消息
要设置每个错误的消息,请在 UserRequest
class
public function messages()
{
return [
'first_name.required' => 'A first name is required',
'first_name.min' => 'The first name can\'t be a single character',
...
];
}
要知道是否有错误,请检查 blade 中的变量 $errors->default
然后您可以显示消息“表单未保存。检查下面的错误并重试”
正如 N69S 指出的那样。我们可以在failedValidation
下设置如下。
/**
* Handle a failed validation attempt.
*/
protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
{
session()->flash('alert-danger', 'There was an error, Please try again!');
return parent::failedValidation($validator);
}