Reputation: 49
I'm working on a project that gets user from the db table. I'm trying to create an update method - SaveUpdatedItems() and I'm incorporating form validation upon submit. But I'm getting MethodNotAllowedHttpExException whenever I submit with incomplete fields. I've also create a method - SaveItems() for saving new record and incorporated the form validation but I'm not getting an error there. I'm fairly new to Laravel. Any ideas why I'm getting the error? Snippet of code below.
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);
}
Routes:
Route::post('/manage/user/save', 'UserController@SaveItems');
Route::post('/manage/user/saveupdated','UserController@SaveUpdatedItems');
update-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
Upvotes: 0
Views: 302
Reputation: 62228
When your validation fails, Laravel will redirect you back to the page that submitted the invalid data. This redirect will be a GET request.
So, if you happen to be on a page that is only accessible via POST, and then you submit a form that has validation errors, you will get redirected back to that page with a GET request, causing a MethodNotAllowedHttpExException
error.
Upvotes: 1