Laravel 5.2验证错误 [英] Laravel 5.2 validation errors
问题描述
我在Laravel 5.2中进行验证时遇到了一些麻烦 当我尝试在像这样的控制器中验证请求时
I have some trouble with validation in Laravel 5.2 When i try validate request in controller like this
$this->validate($request, [
'title' => 'required',
'content.*.rate' => 'required',
]);
Validator捕获错误,但不要将它们存储到会话中,因此当我尝试在模板中调用此代码时
Validator catch error, but don't store them to session, so when i'm try to call in template this code
@if (isset($errors) && count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
Laravel抛出错误
Laravel throw the error
Undefined variable: errors (View: /home/vagrant/Code/os.dev/resources/views/semantic/index.blade.php)
当我尝试使用此代码进行验证
When i'm try validate with this code
$validator = Validator::make($request->all(), [
'title' => 'required',
'content.*.rate' => 'required'
]);
if ($validator->fails()) {
return redirect()
->back()
->withInput($request->all())
->withErrors($validator, 'error');
}
变量$ error在模板中也不可用,但是如果我尝试在控制器中显示错误
Variable $error also not available in template but if i try to display errors in controller
if ($validator->fails()) {
dd($validator->errors()->all());
}
显示错误,但我无法从模板访问它们.
Errors displays but i can't access to them from template.
怎么了?
推荐答案
从Laravel 5.2.27开始更新
Laravel现在默认支持Web中间件,如您在此处看到的:来源
Laravel now supports the web middleware by default as you can see here: source
换句话说,您不再需要将路由包装在Web中间件组周围,因为它可以在RouteServiceProvider文件中为您完成.但是,如果您使用的是5.2.0和5.2.26之间的Laravel版本,请参考以下方法:
In other words, you no longer need to wrap your routes around the web middleware group because it does it for you in the RouteServiceProvider file. However, if you are using a version of Laravel between 5.2.0 and 5.2.26, then refer to the method below:
以下内容仅适用于Laravel 5.2.0至5.2.26
我没有看到您的routes.php
或Kernel.php
文件,这是我所怀疑的情况.
Without seeing your routes.php
or Kernel.php
file, here is what I suspect is happening.
中间件的工作方式已从5.2和5.1更改.在5.1中,您会在app/Http/Kernel.php
文件中看到它:
The way middlewares work has changed from 5.2 and 5.1. In 5.1, you will see this in your app/Http/Kernel.php
file:
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
];
此数组是应用程序的全局HTTP中间件堆栈.换句话说,它们会按每个请求运行.在以下特定中间件上记笔记:Illuminate\View\Middleware\ShareErrorsFromSession
.这就是在每个请求上添加$errors
变量的原因.
This array is your application's global HTTP middleware stack. In other words, they run on every request. Take a note at this particular middleware: Illuminate\View\Middleware\ShareErrorsFromSession
. This is what adds the $errors
variable on every request.
但是,在5.2中,情况已更改,以允许在同一应用程序中同时使用Web UI和API.现在,您将在同一文件中看到它:
However, in 5.2, things have changed to allow for both a web UI and an API within the same application. Now, you will see this in that same file:
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
全局中间件堆栈现在仅检查维护.现在,您有一个名为"web"的中间件组,其中包括大量以前的全局中间件.请记住,这样做是为了在同一应用程序中同时允许Web UI和API.
The global middleware stack now only checks for maintenance. You now have a middleware group called "web" that includes a bulk of the previous global middleware. Remember that it is like this to allow for both a web UI and an API within the same application.
那么我们如何找回$errors
变量?
So how do we get that $errors
variable back?
在路由文件中,需要将路由添加到网络"中间件组中,以便您可以在每次请求时访问该$errors
变量.像这样:
In your routes file, you need to add your routes within the "web" middleware group for you to have access to that $errors
variable on every request. Like this:
Route::group(['middleware' => ['web']], function () {
// Add your routes here
});
如果您不打算构建API,则另一个选择是像5.1中那样将网络"中间件移至全局中间件堆栈.
If you aren't going to build an API, another option is to move the "web" middlewares to the global middleware stack like in 5.1.
这篇关于Laravel 5.2验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!