Reputation: 15
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.
Upvotes: 1
Views: 2731
Reputation: 4235
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);
}
Upvotes: 0
Reputation: 34924
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
Upvotes: 0