1

I have 2 routes:

Route::post('/post_2', 'TestController@post_2')
    ->name('post_2');
Route::post('/result', 'TestController@result')
    ->name('result');

and TestController as below.

public function post_2(){
    return view('post_2View');
}
public function result(\App\Http\Requests\Post_2Request $request){
    return "Successful";
}

Post_2View

<form action="{{route('result')}}" method="post">
           {{ csrf_field() }}
        <input type="text" name="checkValidate">
        <input type="submit" value="Submit">
    </form>

The last is Request to validate checkValidate input in Post_2View

public function rules()
{
    return [
        'checkValidate'=>'required'
    ];
}
public function messages() {
    return [
        'checkValidate.required'=>"This field is required"
    ];
}

When I have data in checkValidate input, everything work fine, but when the Request file return errors, I see the error in my browser

**
 * Throw a method not allowed HTTP exception.
 *
 * @param  array  $others
 * @return void
 *
 * @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
 */
protected function methodNotAllowed(array $others)
{
    throw new MethodNotAllowedHttpException($others);
}

Please tell me, how can I solve it. Thanks.

2 Answers2

0

As you are not sending any data to /post_2 use get verb instead post request

  Route::get('/post_2', 'TestController@post_2')
->name('post_2');

Or you can use both

  Route::match(['get', 'post'],'/post_2', 'TestController@post_2')
->name('post_2');

Because when you come back after validation it is get request

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
  • I still have a problem there, When I try to do it in my project something happend. `This page isn’t working localhost redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS` @user2486 – Cuong V.Nguyen Nov 19 '17 at 09:40
  • What you have changed and in which route you are getting this error? – Niklesh Raut Nov 19 '17 at 11:54
  • When I changed in the example I posted here, It worked fine. But in my real project, the errors happend. – Cuong V.Nguyen Nov 19 '17 at 15:40
  • So definitely there some route related to this route `post_2` or any route which recursively calling in your project, find and check this ? – Niklesh Raut Nov 19 '17 at 15:41
  • This is my route in real project. `Route::match(['get', 'post'],'/questionExcel/add', 'admin\part_3\edit_part_3_test_adminController@add_questionExcel') ->name('part_3.add.questionExcel'); Route::post('/questionExcelConfirm/add', 'admin\part_3\edit_part_3_test_adminController@add_questionExcelConfirm') ->name('part_3.add.questionExcelConfirm');` I read a Excel file and I show it to check before save it in database. If you need more code, I will sent you. Thank @user2486 – Cuong V.Nguyen Nov 19 '17 at 15:48
  • With the example I posted, if I do think you suggest to me, It works fine, but It just happen in my real project. – Cuong V.Nguyen Nov 19 '17 at 15:50
0

I couldn't find the reason why this exception happened, but i found a solution if validation failed don't return MethodNotAllowedHttpException

            $validator = Validator::make($request->all(),[
                'name' => 'required'
            ]);

            if ($validator->fails()) {
                return response()->json(['message'=>'Name field is required'],422);
            }
Mohsen
  • 4,049
  • 1
  • 31
  • 31