strange issues with FormRequest

Posted 3 months 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.

Reply to

Use Markdown with GitHub-flavored code blocks.