2017-10-05 135 views
0

您好我是laravel的新手我正在laravel做一个项目,因为我得到的方法不允许异常验证失败时验证成功,它工作良好。我认为这个问题是验证失败手段页面将被重定向到以前的位置(即编辑表单)作为一个GET请求,所以我的问题是我的编辑页面是在布线后的方法,你可以看我下面的编辑页面的路线方法不允许在laravel验证失败时发生异常

路线:

Route::get('leavelist','[email protected]'); 
Route::post('leaveedit','[email protected]'); 
Route::post('leaveupdate','[email protected]'); 
Route::post('leaveview','[email protected]'); 

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

    } 
...... 

列表页面和隐藏的表单:

<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> 

对于上述从输入值的设置和提交的jquery。

说明的功能:

首先我在离开模块的工作,我在上市列表页应用请假申请的有一个编辑按钮编辑离开请求。当我点击编辑按钮我抓住特定的唯一行ID并提交使用jquery的表单,它的路线是post方法Route::post('leaveedit','[email protected]');然后我的编辑窗体加载正确的值,当我提交编辑窗体来更新路线Route::post('leaveupdate','[email protected]');在更新函数验证通过它是完美的作品如果验证失败,它会重定向到前一页,这是编辑页面,所以问题在这里,而我越来越method not allowed exception我希望我表达了我的问题,如果你有任何澄清问我评论我会回复你。请指导我解决这个问题。

注意:同样的方法工作当直立少数飞蛾现在只有它在我以前开发的所有模块中引发这个错误。我不知道为什么。并且我的laravel版本是Laravel Framework version 5.3.28

+0

我更新了我的问题,你可以请大家看看@iCoders – JYoThI

回答

1

无法使用post方法重定向。通过json响应获取错误消息会更好。

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

.... 
+0

我不是手动重定向。如果验证失败,它会自动重定向到我的编辑表单。并且它不喜欢在url上显示任何id,这就是为什么我要使用post方法提交。 – JYoThI

+0

所以,你根本不需要重定向。对? –

+0

@JYoThI如果验证失败,laravel将重定向到上一页并显示错误消息。如果不需要这个重定向,你可以通过json响应得到相同的消息。这是适合你的吗? –

相关问题