Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

phayes0289's avatar

A Form on a "get" route trouble.

I have created a Laravel dashboard page that contains a bunch of widgets loaded with data returned from a TrainingDashboardController. I have shortened it to one widget for brevity.

    public function index(Request $request)
    {
        
        $request->validate([
            'range_startdt' => 'required|date_format:m/d/Y H:i',
            'range_enddt' => 'required|date_format:m/d/Y H:i',
        ]);
        
    
        $range_startdt = $request->input('range_startdt', date('m/d/Y H:i')); // Set default value if empty
        $range_enddt = $request->input('range_enddt', Carbon::parse($range_startdt)->addDays(7)->format('m/d/Y H:i'));
    
         $ReportCount = Training::whereBetween('startdt', [Carbon::parse($range_startdt)->format('Y-m-d H:i'), Carbon::parse($range_enddt)->format('Y-m-d H:i')])->count();
       
        return view('training.dashboard', compact('ReportCount', 'range_startdt', 'range_enddt'));
    }

The route for this dashboard controller is currently as follows:

Route::get('/training/dashboard', [TrainingDashboardController::class, 'index'])->name('training.dashboard');

On the top of the dashboard page I have a small form with a range start date field and a range end date field. This form is used to filter the data used for the widgets. It looks like this:

            <div class="row">
                <div class="mb-2 col-xl-3 col-md-3"></div>
                <div class="mb-2 col-xl-6 col-md-6 text-center">
                    <label class="form-label col-form-label" for="report_range_start"> Date Range</label>
                    <div class="input-group">
                        <input id="range_startdt" name="range_startdt" class="form-control" type="text"
                            value="{{ $range_startdt }}" style="width:100px">
                        <div class="mt-1 mr-2 ml-2"> to</div>
                        <input id="range_enddt" name="range_enddt"class="form-control" type="text"
                            value="{{ $range_enddt }}" style="width:100px">
                        <div class="input-group-append">
                            <button class="btn btn-primary waves-effect waves-themed" type="submit" id="button-addon5"><i
                                    class="fal fa-arrow-alt-right"></i></button>
                        </div>
                    </div>
                </div>
            </div>
        </form>

I am having an issue with the page when it first tries to load due to the validation code. I am guessing that since the validation code is in there for the form which has not been submitted yet, I am getting this odd behavior of being redirect to various pages.

What is the best way to do this? Should the form be using the "get or post" method? Should the route be using the "get" or "post" method. Any advice would be greatly appreciated!

0 likes
4 replies
LaryAI's avatar
Level 58

The issue you're facing is due to the validation code running when the page first loads. To fix this, you can check if the request method is "GET" before running the validation code. If it is not a "GET" request, then you can proceed with the validation.

Here's an updated version of your index method in the TrainingDashboardController:

public function index(Request $request)
{
    if ($request->method() === 'GET') {
        $request->validate([
            'range_startdt' => 'required|date_format:m/d/Y H:i',
            'range_enddt' => 'required|date_format:m/d/Y H:i',
        ]);
    }
    
    $range_startdt = $request->input('range_startdt', date('m/d/Y H:i')); // Set default value if empty
    $range_enddt = $request->input('range_enddt', Carbon::parse($range_startdt)->addDays(7)->format('m/d/Y H:i'));

    $ReportCount = Training::whereBetween('startdt', [Carbon::parse($range_startdt)->format('Y-m-d H:i'), Carbon::parse($range_enddt)->format('Y-m-d H:i')])->count();

    return view('training.dashboard', compact('ReportCount', 'range_startdt', 'range_enddt'));
}

By checking the request method, the validation code will only run when the form is submitted (i.e., a "POST" request). This will prevent the odd behavior you're experiencing when the page first loads.

Additionally, since you're using a "GET" request for the form submission, you can keep the route as it is:

Route::get('/training/dashboard', [TrainingDashboardController::class, 'index'])->name('training.dashboard');

This way, the form submission will still be handled by the index method in the TrainingDashboardController.

phayes0289's avatar

@LaryAI Nope... That recommended code change does not work. Adding that code to check for the "get" method just puts the page into a loop until it times out. Any other ideas?

phayes0289's avatar
phayes0289
OP
Best Answer
Level 5

@LaryAI This is not pretty or fool-proof, but it does work:

if ($request->filled(['range_startdt', 'range_enddt'])) { $request->validate([ 'range_startdt' => 'required|date_format:m/d/Y H:i', 'range_enddt' => 'required|date_format:m/d/Y H:i', ]); }

I think I am going to rewrite it to insert a default value into the fields but make them read-only. So someone can then use the jquery datatimepicker to change the date, but they cannot type the date. Then I can just remove the validation.

Please or to participate in this conversation.