strange issues with FormRequest

Posted 1 month ago by michaelnguyen547

sometimes, the FormRequest is skipped and the form is rendered again without old inputs.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Enums\MessagePassengerSendTo;

class PassengerMessageForm 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 [
            'subject' => 'required|min:10',
            'content' => 'required|min:10',
            'platform' => 'required',
            'passengerStatus' => 'required_if:sendTo,1'
            'recipients' => 'required_if:sendTo,1',
            'confirm' => 'required_if:sendTo,1'
        ];
    }

}

Here is the controller action

    public function process(PassengerMessageForm $request)
    {
        dd($request->all());
        $message = new Message();
        $message->subject = $request->subject;
        $message->content = $request->content;
        $message->save();

Sometimes, the form is rendered with validation errors. Other times, a new form (without old inputs and validation errors) is rendered.

I intend to have the form fails validation. Even I have the dd($request->all()), it does not reach. Either it will render the form with validation or it will render a new form again.

Updated: I have the same issue with other validation method

        $validatedData = $request->validate([
            'subject' => 'required|min:10',
            'content' => 'required|min:10',
            'platform' => 'required',
            'passengerStatus' => 'required_if:sendTo,1',
            'recipients' => 'required_if:sendTo,1',
            'confirm' => 'required_if:sendTo,1' 
        ]);

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.