2

Hi I am new to laravel i'm doing one of project in laravel on that i'm getting method not allowed exception when validation failed when validation success it works good . i think the problem is validation failed means page will be redirected to previous location (i.e edit form ) as a get request so my problem is my edit page is post method in route can you take look below route of my edit page

Route :

Route::get('leavelist','LeaveController@index');
Route::post('leaveedit','LeaveController@edit');
Route::post('leaveupdate','LeaveController@update');
Route::post('leaveview','LeaveController@view');

LeaveController :

.......
public function update(Request $request){

        $this->validate($request,[ 'request_type'=>'required',
                                   'description'=>'required',
                                   'from'=>'required',
                                   'to' => 'required',
                                   'requested_to' => 'required',
                                   'status' => 'required'
                                 ]);

        $leave_update = LeaveModel::find($request->id);

        $leave_update->request_type=$request->request_type;
        $leave_update->requested_person=Auth::user()->id;
        $leave_update->requested_to=$request->requested_to;
        $leave_update->from=date('Y-m-d h:m:00',strtotime($request->from));
        $leave_update->to=date('Y-m-d h:m:00',strtotime($request->to));
        $leave_update->description=$request->description;
        $leave_update->status=$request->status;
        $leave_update->updated_by=Auth::user()->id;

        if($leave_update->save()){
            //have to change this later
            return redirect('leavelist')->with('message',
                                                         '<br><div class="alert alert-success alert-dismissable">
                                                          <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
                                                          <strong>Success!</strong> Leave request Updated succesfully... </div>'
                                                         ); 

        }else{
            //have to change this later
            return redirect('leavelist')->with('message',
                                                         '<br><div class="alert alert-warning alert-dismissable">
                                                          <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
                                                          <strong>Warning!</strong> Leave request Updated failed... </div>'
                                                         );
        }

    }
......

List page with and hidden form :

    <table class="table table-striped table-bordered table-hover" id="sample_2">
<thead>
    <tr>
        <th> S.no</th>
        <th> Name</th>
        <th> Department</th>
        <th> Request Type</th>
        <th> Request To</th>
        <th> Responded Person</th>
        <th> From</th>
        <th> To</th>
        <th> Status</th>
        <th> Action</th>
    </tr>
</thead>
<tbody>
<?php $i=1; ?>
@forelse ($leave_list as $key=>$value) 

    <tr>
        <td> {{ $i }} </td>
        <td> {{ $value->requested_person_name }} </td>
        <td> {{ $value->dep_name }} </td>
        <td> {{ $value->request_type_name }} </td>
        <td> {{ $value->requested_to_person }} </td>
        <td> {{ $value->responded_person_name }} </td>
        <td> {{ date('d-m-Y h:m',strtotime($value->from)) }} </td>
        <td> {{ date('d-m-Y h:m',strtotime($value->to)) }} </td>
        <td> {{ $value->status }} </td>
        <td>
            <a href="#" data="{{ $value->id }}" onclick="event.preventDefault(); val = this.getAttribute('data'); document.getElementById('edit_unique_id').value=val;  document.getElementById('edit-form').submit();  " class="btn btn-circle btn-icon-only green">
                 <i class="fa fa-edit"></i>
            </a>
            <a href="#" data="{{ $value->id }}" onclick="event.preventDefault(); val = this.getAttribute('data'); document.getElementById('view_unique_id').value=val;  document.getElementById('view-form').submit();  "class="btn btn-circle btn-icon-only blue">
                 <i class="fa fa-file-o"></i>
            </a>
        </td>
    </tr>

<?php $i++; ?>
@empty

@endforelse

</tbody>
</table>
<form id="edit-form" action="{{ url('leaveedit') }}" method="POST" style="display: none;">

    {{ csrf_field() }}

<input type="hidden" name="id" id="edit_unique_id" value="" />

</form>

For the above from input values are set and submit by jquery .

Explanation of functionality :

First of all I'm working in leave module and i'm listing the applied leave request in list page there was a edit button to edit the leave request .when i click the edit button i'm grabbing particular unique row id and submiting the form using jquery and it's route is post method Route::post('leaveedit','LeaveController@edit'); then my edit form loads with right values when i submit the edit form to update route Route::post('leaveupdate','LeaveController@update'); In update function validation pass it's works perfect if validation fails it will redirect to previous page which is edit page so problem is here while i'm getting method not allowed exception i hope i conveyed my problem if you have any clarification ask me in comment i will reply you. Please guide me to solve this problem .

Note : The same approach is worked prefect few moths back now only it's throwing this error in all i previously developed modules. i don't know why . and my laravel version is Laravel Framework version 5.3.28

1
  • I updated my question can you please take a look @iCoders Commented Oct 5, 2017 at 4:35

1 Answer 1

1

It is not possible to redirect with a post method. it is better to get the error messages through json response.

$validator = Validator::make($request->all(), [
            'request_type'=>'required',
            'description'=>'required',
            'from'=>'required',
            'to' => 'required',
            'requested_to' => 'required',
            'status' => 'required
        ]);    


if ($validator->fails()) {    
    return response()->json($validator->messages(), 200);
}

....
Sign up to request clarification or add additional context in comments.

11 Comments

I'm not redirecting manually .if validation failed it's automatically redirected to my edit form . and it don't like to show any id on url that's why i'm submiting the from using post method .
@JYoThI If validation fails, laravel wil redirect to the previous page with an error messages. If do not need this redirection, you can get the same messages through json response. is that works for you?
Did you notice I didn't redirect manually when validation failed its automatically redirect to previous page I think it's default behaviour of laravel validation. The problem is when it's back to previous with error message while throwing that error method not allowed.
Yes, it is. and what do you need when validation fails?
I need to redirect to previous page with validation error messages. And route should be post method for my edit gage.
|

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.