1

The following is my blade :

<form action="{{route('ans1.eval')}}" method="post">
    <br>
    <input type="radio" name="evaluate" class="evaluate" value=10> 1   &nbsp; &nbsp;
    <input type="radio" name="evaluate" class="evaluate" value=15> 1.5 &nbsp; &nbsp;
    <input type="radio" name="evaluate" class="evaluate" value=20> 2   &nbsp; &nbsp;
    <input type="radio" name="evaluate" class="evaluate" value=25> 2.5 &nbsp; &nbsp;
    <input type="radio" name="evaluate" class="evaluate" value=30> 3   

    <button type="submit" class="btn btn-primary" align="right">Evaluate Answer</button>
    <input type="hidden" value="{{ Session::token() }}" name="_token">
</form>

The following is my route :

Route::post('/evaluateans', [
    'uses' => 'AnswerController@postEvaluateAns',
    'as' => 'ans1.eval',
    'middleware' => 'auth'
]);

The following is my validation :

public function postEvaluateAns(Request $request)
{
    $this->validate($request, [
          'evaluate' => 'required'
    ]);
}

The following is the error when no evaluation is selected :

MethodNotAllowedHttpException in RouteCollection.php line 218

1 Answer 1

1

From the laravel docs on validation

If the validation rules pass, your code will keep executing normally; however, if validation fails, an exception will be thrown and the proper error response will automatically be sent back to the user. In the case of a traditional HTTP request, a redirect response will be generated.

When your validation fails it redirects back with a GET method (a redirection uses the GET method) but if you show the form from a route that is not a GET one it throws this error.

So you have to show the form with a GET route.

As alternative you can manually create your validator so you can choose the redirect GET route in case of failed validation, e.g.:

public function postEvaluateAns(Request $request)
{
    $validator = Validator::make($request->all(), [
        'evaluate' => 'required'
    ]);

    if ($validator->fails()) {
        return redirect('failed_validation_GET_route')
                ->withErrors($validator)
                ->withInput();
    }
    return redirect()->route('success_GET_route')
                     ->with('status', 'Success!');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.