1

フォームの検証が失敗したときに、フラッシュ メッセージを設定して渡そうとしています。検証に合格すると、コントローラに Flash メッセージを設定できます。

をオーバーライドしようとしましたprotected failedValidation()が、エラーが発生しました。

<?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\Http\Requests\Flash' が見つかりません

次のように app.blade.php でビューを設定しています

<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 -->
4

1 に答える 1