我想在用戶給出錯誤的輸入時在View頁面顯示驗證錯誤。當用戶輸入錯誤時,它不會在數據庫中保存任何內容。但用戶視圖頁面中沒有錯誤消息。如果有人發現錯誤,請幫我解決。Laravel 5.2驗證錯誤沒有出現在刀片上
這裏是控制器:
public function saveUser(Request $request){
$this->validate($request,[
'name' => 'required|max:120',
'email' => 'required|email|unique:users',
'phone' => 'required|min:11|numeric',
'course_id'=>'required'
]);
$user = new User();
$user->name= $request->Input(['name']);
$user->email= $request->Input(['email']);
$user->phone= $request->Input(['phone']);
$user->date = date('Y-m-d');
$user->completed_status = '0';
$user->course_id=$request->Input(['course_id']);
$user->save();
return redirect('success');
}
這裏是路線:
Route::group(['middleware' => 'web'], function() {
Route::get('/', function() {
return view('index');
})->name('main');
Route::post('/saveUser',[
'uses' => '[email protected]',
'as'=>'saveUser',
]);
});
這裏是葉片視圖頁:
@extends('layouts.master')
@section('title')
Create User
@endsection
@section('content')
@include('partials.message-block')
<div class="container" >
<h3> Student Register </h3>
{!! Form::open(array('route' => 'saveUser','class'=>'form-horizontal','method'=>'POST')) !!}
{!! Form::token(); !!}
{!! csrf_field() ; !!}
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" required placeholder="Name">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="email" class="form-control" required placeholder="email">
</div>
<div class="form-group">
<label>Phone Number</label>
<input type="text" name="phone" class="form-control" required placeholder="phone">
</div>
<div class="form-group">
<label for="">Class</label>
<select class="form-control input-sm" name="course_id" >
@foreach($input as $row)
<option value="{{$row->id}}">{{$row->name}}</option>
@endforeach
</select>
</div>
<button type="submit" class="btn btn-default">Submit</button>
{!! Form::close() !!}
</div>
@endsection
這裏是錯誤消息塊:
@if(count($errors) > 0)
<div class="row">
<div class="col-md-4 col-md-offset-4 error">
<ul>
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
</div>
@endif
@if(Session::has('message'))
<div class="row">
<div class="col-md-4 col-md--offset-4 success">
{{Session::get('message')}}
</div>
</div>
@endif
如果您使用的是5.2.27或更高版本,請嘗試刪除'web'中間件。 –
感謝您的幫助,現在正在工作! :) – User57
[ErrorBag在Laravel 5.2中始終爲空]的可能重複(http://stackoverflow.com/questions/36377420/errorbag-is-always-empty-in-laravel-5-2) – patricus