mardi 17 septembre 2019

Flash Error message when Validation Fails while using FormResquest

I am using FormRequest to do my validations. I am trying to set a top-level error message via Flash to show to the user that the form submission did not succeed.

Right now I have the following 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'
        ];
    }

I am trying to do this in my Controller

$validator = $request->validated();

        if ($validator->fails()) {
            Session::flash('error', $validator->messages()->first());
            return redirect()->back()->withInput();
        }

But the Flash message is turning up empty. whats the best way to set the Flash Error message when using FormRequest



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire