繁体   English   中英

MethodNotAllowedHttpException($others); Validate 中出现错误时 (Laravel 5.5)

[英]MethodNotAllowedHttpException($others); When have errors in Validate (Laravel 5.5)

我有2条路线:

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

和 TestController 如下。

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>

最后是请求验证 Post_2View 中的 checkValidate 输入

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

当我在 checkValidate 输入中有数据时,一切正常,但是当请求文件返回错误时,我在浏览器中看到错误

**
 * 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);
}

请告诉我,我该如何解决。 谢谢。

由于您没有向/post_2发送任何数据, /post_2使用get动词代替post请求

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

或者你可以同时使用

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

因为当您在验证后回来时,它是获取请求

我找不到这个异常发生的原因,但我找到了一个解决方案,如果验证失败不返回MethodNotAllowedHttpException

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM