5

I'm trying to validate input field but, when I use the validation rules it throw a: The GET method is not supported for this route. Supported methods: POST. error.

This is my controller

public function delivery(Request $request)
{
    $request->validate([
        'code' =>'required|numeric|size:4'
    ]);

    return view('frontEnd.orderInfo');
}

This is the route :

Route::post('/delivery','orderController@delivery');

This is the view

<form id="loginform" action="{{url('delivery')}}" class="form-horizontal" method="post" role="form" enctype="multipart/form-data">
    @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif 
    {{method_field('POST')}}
    {{ csrf_field() }}   
    <div style="margin-bottom: 25px" class="input-group">
        <span class="input-group-addon"><i class="glyphicon glyphicon-phone"></i></span>
        <input  type="password" class="form-control" name="code" value="" placeholder="enter code.">
    </div>    
    <div style="margin-top:10px" class="form-group">
        <!-- Button -->
        <div class="col-sm-12 controls">
            <input type="submit" class="btn btn-success" value="submit"/>          
        </div>
    </div>  
</form>
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
hala
  • 161
  • 2
  • 13

2 Answers2

1

You should make sure you display the form in the route using GET method. When you use validation, if validation fails, Laravel makes redirection (using GET method) to the route where form was displayed. So in such case when you have multiple steps form, you should put data into session and handle also GET method for steps where you use validation.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • 1
    I did something like this '$validator = Validator::make($request->all(), [ 'code' =>'required|numeric|size:4' ]); if ($validator->fails()) { Session::flash('error', $validator->messages()->first()); return redirect()->back()->withInput(); }' but it gave me the same error . although i tried to change the route from post to get, it gave me the same error and , considered the REQUEST_METHOD "POST" ,not get @Marcin Nabiałek – hala Aug 20 '19 at 15:47
0

If you have a multi-step form, and you need to validate on the back end, the best way to do it in this case is to use ajax to post your forms. For instance, if you are using axios.js:

let form = document.getElementById('myForm');
form.addEventListener('submit', (evt) => {
 evt.preventDefault();
 let formData = new FormData(form);

 axios.post('url/to/controller', formData)
 .catch((err) => {
  let e = err.response ? err.response.data : null;
  e = e.errors ? e : null;
  if( e ) {
    let formErrors = err.response.data.errors;
    let errorMessage = err.response.data.message;
    // populate form with error messages...
  } else {
    console.log( err.message );
  }
 })
 .then((response) => {
  if( response && response.data ) {
   // success, next step or whatever
  }
 }); // end axios post
}); // end event listener

Using the GET method for most forms is rarely a good solution, nor is saving the information in the Session.

Dan S
  • 99
  • 6