0

What I am trying to achieve is to redirect back to the page with validation message if the input is incorrect. Currently I am facing an error and that says.

Method Not Allowed

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for route /profile/details_options Supported methods: POST.

first in my web.php

// this is the current page basically in this page I am viewing a form wherein it will check if the employee username is valid or not...
Route::view('/check_uname', 'template.viewhere')->name('route.name');

// endpoint of the form placed in the /check_uname uri 
Route::post('/profile/details_options', [FirstController::class, 'firstEndpointFunction'])->name('uname.details_options');

// endpoint of the form return view of the route('uname.details_opitons) which is the page2.displayPage
Route::post('/profile/verify_changes', [SecondController::class, 'secondEndpointFunction'])->name('verify.detail_changes');

in the check_uname page I do have a form, and in this form I do have the endpoint of it. so the form looks like

<form action="{{ route('uname.details_options')}}">
<-- inputs should be here -->
</form>

and now in my FirstController.php controller.

FirstController.php

public function firstEndpointFunction(Request $request)
    {
       
    // this is where the logic goes and if the condition is true then i will fetch the data and 
    // pass it on the page2.DisplayPage
    if(true){
    return view('page2.displayPage', [
                'data' => $data,
                'data2' => $data2,
                'data3' => $data3
            ]);
    }
    else {
    }
    }

and now the since the condition is true then it returned the view of page2.displayPage with the data that I have passed but the url still remains the same which is the POST route URL: /profile/details_options

so in this page2.displayPage now I also have a form again

<form action="{{ route('verify.detail_changes')}}">
<-- inputs should be here -->
</form>

Now in the SecondController.php Controller is where I am having troubled at and the the controller for this one for its endpoint is:

SecondController.php

public function secondEndpointFunction(Request $request)
    {
       
    if(true){
       return response()->json(['message' => 'The input is correct.']);
    }
    else{
    // throws the error I have mentioned aove. When the users input does not match in the DB
    // my main goal is to I want to display an error message to the user that the input
    // does not match in the database.
       return redirect()->back()->withInput()->withErrors(['error' => 'Nothing has found.']);

}

In the last Controller the OtherController.php is where I am having troubled at whenever the user inputs a wrong input that does not match in the database, it throws me an error, the error that I have indicated above.

things I did to fix were the ff:

php artisan route:cache
php artisan route:clear

but nothing has worked.

Hoping to get an idea on how to fix this one. Thank you!

4
  • do not show any view in POST route, use it for only validating data and saving and use GET route for showing form and any intended errors! Although, you can check this answer to achieve what you're looking for but it's not recommended !
    – OMi Shah
    Commented Jun 24 at 9:02
  • Thanks for the suggestion @OMiShah . By any chance do you have any reference that you use for what you have suggested? for only POST validating and GET for viewing? I would appreciate it.
    – alex wang
    Commented Jun 24 at 9:09
  • Technically, I want to validate the username first, so I want to fetch the username in the firstpage and display it on the second page and in the secondpage, I want to verify another thing as well, let's say verify1 so that I have both username and the verify1 correct. to sum it up, I need to verify both on two different pages. so I could proceed to the last page.
    – alex wang
    Commented Jun 24 at 9:13
  • Check the code as in question: stackoverflow.com/questions/22371630/… ... you can also check this ext link: multi-page-step-form-in-laravel-with-validation/
    – OMi Shah
    Commented Jun 24 at 9:14

1 Answer 1

0

You have this html form in your view:

<form action="{{ route('verify.detail_changes')}}">
<-- inputs should be here -->
</form>

From https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/method

The HTMLFormElement.method property represents the HTTP method used to submit the <form>.
Unless explicitly specified, the default method is 'get'

so if the route you are submitting to is a post/put/patch route, you need to specify method="POST" in your form tag. Since this is laravel you also need to include the csrf token, and sometimes the _method too

<form action="{{ route('verify.detail_changes')}}" method="POST">
@csrf
{{-- if route is put/patch --}}
{{-- @method('PUT') or @method('PATCH') --}}
<-- inputs should be here -->
</form>
2
  • Hello kris, I have added the method="POST"> in form tag however it still display the same error. I have added it in both form.
    – alex wang
    Commented Jun 24 at 12:26
  • do not return views from post requests, either return redirects or json.
    – kris gjika
    Commented Jun 24 at 12:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.