首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用FormRequest设置闪存错误消息

用FormRequest设置闪存错误消息
EN

Stack Overflow用户
提问于 2019-09-17 07:58:45
回答 1查看 612关注 0票数 1

当表单验证失败时,我试图设置并传递一条闪存消息。验证通过后,我可以在控制器中设置Flash消息。

我试图重写protected failedValidation(),但是我得到了一个错误。

代码语言:javascript
复制
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest; use Illuminate\Support\Facades\Validator;

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'
        ];
    }

    /**
     * Handle a failed validation attempt.
     */
    protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
    {
        Flash::error("Profile could not be saved!");
        // alert()->error('oops ... error');
        return parent::failedValidation($validator);
    } }

错误:

找不到Symfony\Component\Debug\Exception\FatalThrowableError类‘App\

\\’

我在app.blade.php中将我的观点设置为

代码语言:javascript
复制
<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">&times;</a></p>
                    @endif
                @endforeach
            </div> <!-- end .flash-message -->
EN

回答 1

Stack Overflow用户

发布于 2019-09-17 08:03:48

您需要在控制器中添加use Flash;,该控制器位于名称空间声明后的顶部,您希望在其中使用闪存库方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57969804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档