0

I am getting data from backend and I want users to verify and update. What I am doing is passing the data from controller and fill them inside a form in view blade. When the user verifies the data and submit them, I pass them to validation in my method inside controller. As soon as the validation start laravel throws error:

\Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException", "The GET method is not supported for this route. Supported methods: POST.

Below is one the method in FormsController.php passing the variable that have been collected from database to view forms.oneedit.blade.php

public function editingone(Request $request)
    {
        $nameinst =  $request->nameinst;
        $firstnm  =  $request->firstnm;
        $lastnm  =  $request->lastnm;
        $phn  =  $request->phn;
      
        $myArray['nameinst'] = $nameinst;
        $myArray['firstnm'] = $firstnm;
        $myArray['lastnm'] = $lastnm; 
        $myArray['phn'] = $phn;

        return view('forms.oneedit', $myArray);
   
    }

Below I receive the data in forms.oneedit.blade.php in view

<form class="w-full max-w-lg border-4 rounded-lg p-2" action="{{ route('updateeditedone.fomrone') }}" method="post">
@csrf
      <input class="@error('firstname') border-red-500 @enderror"
        id="firstname" name="firstname" type="text"  value="{{ old('firstname', $firstnm) }}" required>
             @error('firstname')
                <div class="text-red-500 mt-2 text-sm">
                    {{ $message }}
                </div>
            @enderror
         ......................
      <input class="
       @error('lastname') border-red-500 @enderror"
        id="lastname" name="lastname" type="text" value="{{ old('lastname', $lastnm) }}" required>
        @error('lastname')
                <div class="text-red-500 mt-2 text-sm">
                    {{ $message }}
                </div>
            @enderror
    ......................

    <button type="submit">Submit</button> </form>

Below are among the routes in web.php

Route::post('/editone/formone', [FormsController::class,'editingone'])->name('edit.fomrone');
Route::post('/update/edited/formone', [FormsController::class,'updateeditedngone'])->name('updateeditedone.fomrone');

Below is the method that I am trying to validate the values in FormsController.php where the error occurs

  public function updateeditedngone(Request $request)
    {
    $this->validate($request, [
        'nameinstitute'=> 'required|max:255',
        'firstname' => 'max:255',
        'lastname' => 'max:255',
        'phone'  => 'required|max:255',
    ]); }

NB: If I remove the validation process inside the controller and just get the value it works, something like below:

    $val = $request->nameinstitute;
    dd($val);

With the above I correctly get the values before validation, But if I try to validate them first the error is thrown. Thanks in advance.

Update:

I have edited the validation method so as to direct to a certain view as suggested but still the same error

public function updateeditedngone(Request $request)
    {
        $validator = Validator::make($request->all(), 
        [ 'nameinstitute'=> 'required|max:255','firstname' => 'max:255',
         'lastname' => 'max:255',
         'phone'  => 'required|max:255']); 

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

        dd($validator);
        return redirect()->route('form.checkbtn');
Gabriel Rogath
  • 710
  • 2
  • 8
  • 23
  • 1
    Think through what happens when validation fails - Laravel redirects back to the form, using a GET request. But there is no GET route for your form, only POST. – Don't Panic Aug 12 '22 at 07:45
  • Does this answer your question? [validation inputs throw a The GET method is not supported for this route. Supported methods: POST. error](https://stackoverflow.com/questions/57548210/validation-inputs-throw-a-the-get-method-is-not-supported-for-this-route-suppor) – Don't Panic Aug 12 '22 at 07:45
  • What if the I am sure of the inputs that will not cause validation failure and still get the same error? Thanks in advance @Don'tPanic, – Gabriel Rogath Aug 12 '22 at 08:23
  • The most likely answer is that your assumptions about your inputs are wrong :-) Or maybe your validation is not working as you expect. And in any case, even when everything is working correctly, it can happen in future that your validation will fail, right? So you need a GET route, no matter what. Add it, look at the errors, you'll find out what the problem is, and you can move forward ... – Don't Panic Aug 12 '22 at 09:28
  • okay, so how do I set GET and POST together in one form, or do you mean I should change the form method to GET instead of POST? I am new to laravel I am trying to learn. Thanks @Don'tPanic – Gabriel Rogath Aug 12 '22 at 09:33
  • Look at the error msg; think about what is happening. The browser is trying to GET `/editone/formone`, but there is no route for that. So you need to add a route, like `Route::get('/editone/formone', ...)`. If you're learning, [read the docs](https://laravel.com/docs/9.x/validation), and search here on SO - there are *many* others who have faced this problem before and the answers are all here :-) Check the duplicate I already linked, and others, eg https://stackoverflow.com/q/67846979/6089612 ... – Don't Panic Aug 12 '22 at 09:39

1 Answers1

-1
php artisan view:clear   
 php artisan cache:clear
 php artisan route:clear

Run it from CMD

It works for me