Laravel 5.2 验证错误未出现在刀片中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36784253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.2 Validation Error not appearing in blade
提问by User57
I want to show validation Error in View page while user giving wrong input. It's Ok that it's not saving anything in database while a user giving wrong input. But there is no error message in user view page. If anyone find the error, please help me out.
当用户输入错误时,我想在视图页面中显示验证错误。当用户输入错误时,它不会在数据库中保存任何内容,这没关系。但是在用户视图页面中没有错误消息。如果有人发现错误,请帮助我。
Here is the controller:
这是控制器:
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');
}
Here is the route:
这是路线:
Route::group(['middleware' => 'web'], function () {
Route::get('/', function () {
return view('index');
})->name('main');
Route::post('/saveUser',[
'uses' => 'AppController@saveUser',
'as'=>'saveUser',
]);
});
Here is the blade view page:
这是刀片视图页面:
@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
And here is the error-message block:
这是错误消息块:
@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
回答by Alexey Mezenin
Try to remove web
middleware if you're using 5.2.27 or higher. The thing is now Laravel automatically applies web
middleware to all routes inside routes.php
and if you're trying to add it manually you can get errors.
web
如果您使用的是 5.2.27 或更高版本,请尝试删除中间件。现在的事情是 Laravel 自动将web
中间件应用于内部的所有路由routes.php
,如果您尝试手动添加它,则可能会出错。
app/Providers/RouteServiceProvider.php
of the 5.2.27 version now adds web middleware to all routes inside routes.php
:
app/Providers/RouteServiceProvider.php
5.2.27 版本现在将 web 中间件添加到所有路由中routes.php
:
protected function mapWebRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web',
], function ($router) {
require app_path('Http/routes.php');
});
}
回答by Kanu
Use Below line in your controller
在控制器中使用下面的行
use Validator;
使用验证器;
Add below code in your controller function where your request is sent.
在发送请求的控制器函数中添加以下代码。
$validator = Validator::make($request->all(), [
'fname' => 'required|max:20|min:4',
'uemail' => 'required|email',
'message' => 'required',
]);
if ($validator->fails()) {
$messages = $validator->messages();
return Redirect::back()->withErrors($messages)->withInput($request->all());
}
In your view page
在您的查看页面中
@if ($errors->any())
<label for="fname" class="error">{{ $errors->first('fname') }}</label>
@endif
For display individual field wise error.
用于显示单个字段明智的错误。
回答by Hassaan Ali
Alexey is correct and if you're not comfortable with that then you can just add this code into your view for the session messages to show.
Alexey 是正确的,如果您对此不满意,那么您可以将此代码添加到您的视图中以显示会话消息。
@if(Session::has('message'))
<div class="alert alert-success">{{Session::get('message')}}</div>
@endif
@if(count($errors)>0)
<ul>
@foreach($errors->all() as $error)
<li class="alert alert-danger">{{$error}}</li>
@endforeach
</ul>
@endif
I've done this in laravel 5.5. Please do confirm if this helps you out.
我已经在 Laravel 5.5 中做到了这一点。请确认这是否对您有帮助。