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

ilarioengler's avatar

Laravel 5 - MethodNotAllowedHttpException when validation is false

Always when the validation fails, I get a MethodNotAllowedHttpException

routes.php

Route::post('download',  'UrlController@download'); 
Route::post('search', 'UrlController@search');

UrlController.php

public function download(DownloadRequest $request)
{
    dd($request->all());
}

DownloadRequest.php

public function authorize()
{
    return true;
}

public function rules()
{
    return [
        'format' => 'required|between:1,13'
    ];
}

name.blade.php

{!! Form::open(['url' => 'download']) !!}

{!! Form::select('format', [
    'Please select format',
    'FormatGrp1' => [1 => 'best', 'p1','p2', 'p3', 'p4'],
    'FormatGrp2' => [6 => 'p5', 'p6']
]) !!}

When "Please select format" is chosen and the form is submitted, I always get this error because "Please select format" has value 0 and i specified values must be between 1 and 13. (Look at DownloadRequest.php)

Thanks for help!

0 likes
6 replies
petrit's avatar

Try by changing to

{!! Form::open(['action' => 'UrlController@download', 'method' => 'post']) !!}

Or, I would try by using html traditional tags

<form action='/download' method='post'>
...
</form>
ilarioengler's avatar
ilarioengler
OP
Best Answer
Level 3

The error didn't come from the validation.

It was because it called the URL to go back and display the errors. And this is the search method.

So cause of the logic in search method the exception has been thrown.

But thank you for your comment :)

stueynet's avatar

Hi @ilarioengler. Sorry to resurrect this thread but could you explain a bit further? I have a simple inline validation as follows:

My form:

    <form action="{{url('admin/users/search')}}" method="post">
        {{csrf_field()}}
        <input name="keywords" type="text" class="form-control" placeholder="Search users">
    </form>

My method:

    public function searchUsers(Request $request)
    {
        $this->validate($request, ['keywords' => 'min:3']);
        $keywords = $request->input('keywords');
        $users    = User::SearchByKeyword($keywords)->paginate(100);

        return view('admin/users/index', compact('users'));
    }

When I search from the admin/users page and it validates, it ends up at the /admin/users/search url. I assume the issue is that in my search method I need to redirect? Though how would I redirect with the results of the search. Ie with the $users object in tow?

Thanks!

iscromanpc's avatar

I'm having the same issue.

Form:

Validation: $this->validate($request, [ 'password' => 'required|string|min:6|confirmed|regex:/^[a-zA-Z0-9_.-]*$/', ]);

Route: Route::post('/save_new_password/', 'ForgotCredentialsController@save_new_password')->name('save_new_password');

Error after fail in validation of password: RouteCollection->methodNotAllowed(array('POST'))

Currently searching for answers.

moa101's avatar

When you have this Error, In your if ($validator->fails()) { } consider that you are going to open the view you are working on for the first time, and add ->withErrors($validator). For exemple :

public function edit($id)
{
            $exams = Exam::all();
            return view('exams.index', compact("exams"));
}
...
public function update(Request $request,$id)
{
     $validator = Validator::make($request->all(),[
            'start' => 'required',
            'end' => 'required|after:start'
        ]);

     if ($validator->fails()) 
     {
      $exams = Exam::all();
      return view('exams.index', compact("exams"))->withErrors($validator);
     }

    //Your update code if validator not fails
}
Snapey's avatar

The answer to this problem is to make sure you always return redirect to a GET route after any form POST.

If validation fails, you will be redirected to the previous page with a GET request. Redirections are ALWAYS GET not POST so if your form (the thing that is being validated) is being shown as the result of a POST then any validations on this form will go to the wrong route.

Please or to participate in this conversation.