我正在开发一个从db表中获取用户的项目。我正在尝试创建更新方法 - SaveUpdatedItems() ,并且我在提交时合并了表单验证。但是每当我提交不完整的字段时,我都会收到 MethodNotAllowedHttpExException 。我还创建了一个方法 - SaveItems() ,用于保存新记录并合并表单验证,但我没有收到错误。我对Laravel来说还算新手。我有什么想法得到错误?下面的代码片段。
public function ValidateFormInput($request){
return $this->validate($request, [
'name' => 'required',
'email' => 'required',
'password' => 'required',
'role' => 'required',
]);
}
public function SaveUpdatedItems(Request $request){
$this->ValidateFormInput($request);
}
public function SaveItems(Request $request){
$this->ValidateFormInput($request);
}
路线:
Route::post('/manage/user/save', 'UserController@SaveItems');
Route::post('/manage/user/saveupdated','UserController@SaveUpdatedItems');
更新user.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<div class="navbar-container">
{!! Menu::get('MyNavBar')->asUl(
['class' => 'nav navbar-nav nav-pills'],
['class'=>'dropdown-menu']
) !!}
</div>
</div>
<div class="panel-body">
<h2>Update User</h2>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
{{Form::open(array('action' => 'UserController@SaveUpdatedItems', 'method' => 'post','files' => true))}}
{{ Form::hidden('_token', csrf_token() ) }}
@foreach($items as $item)
{{ Form::hidden('id[]', $item->id) }}
<table class="table table-striped table-bordered table-hover table-sm">
<tbody>
<tr>
<td>Name:</td>
<td>{{ Form::text('name', $item->name ,array('class' => 'form-control')) }}</td>
</tr>
<tr>
<td>Email:</td>
<td>{{ Form::text('email', $item->email, array('class' => 'form-control')) }}</td>
</tr>
<tr>
<td>Password:</td>
<td>{{ Form::password('password', array('class' => 'form-control')) }}</td>
</tr>
<tr>
<td>Role:</td>
<td>{{Form::select('role', $roles , null ,['class' => 'form-control'])}}</td>
</tr>
<tr>
<td>Active (?):</td>
<td>{{Form::select('is_active' , array('' => '-----', 1 => 'Yes', 0 => 'No'), $item->is_active,['class' => 'form-control'])}}</td>
</tr>
<tr>
<td>Aprroved (?):</td>
<td>{{Form::select('is_approved', array('' => '-----', 1 => 'Yes', 0 => 'No'), $item->is_approved, ['class' => 'form-control'])}}</td>
</tr>
</tbody>
</table>
@endforeach
<button type="submit" class="btn btn-primary">Submit</button>
{!! Form::close() !!}
<br>
<a href="/manage/user"> <button type="submit" class="btn btn-primary">View Users</button></a>
</div>
</div>
</div>
</div>
</div>
@endsection
@section('scripts')
<script type="text/javascript">
$(document).ready(function(){
});
</script>
@endsection
答案 0 :(得分:1)
当验证失败时,Laravel会将您重定向回提交无效数据的页面。此重定向将是GET请求。
因此,如果您恰好位于只能通过POST访问的页面上,然后您提交了一个包含验证错误的表单,您将通过GET请求重定向回该页面,从而导致{{1}错误。