[英]Laravel validation don't pass to blade
I pulled project from repository to local environment.我将项目从存储库拉到本地环境。 I face a problem that validation messages don't display on blade.
我面临一个问题,即验证消息不显示在刀片上。
In my controller function, I can dump validation errors like this:在我的 controller function 中,我可以转储如下验证错误:
$validator = Validator::make($request->all(), [
'first_name' => 'required|min:3',
]);
if ($validator->fails()) {
dd($validator);
return redirect()->back()->withErrors($validator)->withInput();
}
I took some time to search what can cause this issue and I found some results that my issue can be associated with web middleware.我花了一些时间搜索可能导致此问题的原因,并发现一些结果表明我的问题可能与 web 中间件有关。
Originally, my files looks like this:最初,我的文件如下所示:
RouteServiceProvider RouteServiceProvider
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
Kernel Kernel
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
web.php web.php
Route::get('register', 'Auth\RegisterController@getRegister');
Route::post('register', 'AuthController@postRegister');
I tried to wrap my routes in web middleware like this:我试图将我的路由包装在 web 中间件中,如下所示:
Route::group(['middleware' => ['web']], function () {
Route::get('register', 'Auth\RegisterController@getRegister');
Route::post('register', 'AuthController@postRegister');
});
Also I tried to move StartSession::class and ShareErrorsFromSession::class from middlewareGroups to middleware and removed middleware('web') from mapWebRoutes and problem still persists.我还尝试将 StartSession::class 和 ShareErrorsFromSession::class 从 middlewareGroups 移动到中间件,并从 mapWebRoutes 中删除中间件('web'),问题仍然存在。 Maybe someone faced same issue and was able to solve it?
也许有人面临同样的问题并且能够解决它?
My Laravel version is: 5.7.29, php version: 7.1.33我的 Laravel 版本是:5.7.29,php 版本是:7.1.33
Validation working on production生产验证工作
Instead of giving back the $validator object you should return the MessageBag
which you can get with $validator->errors()
.而不是返回 $validator object 您应该返回可以使用
$validator->errors()
获得的MessageBag
。
So your return statement will look like this:因此,您的退货声明将如下所示:
return redirect()->back()->withErrors($validator->errors())->withInputs()
The MessageBag looks like this ['attrXY' => [error1, error2, ...] ]
. MessageBag 看起来像这样
['attrXY' => [error1, error2, ...] ]
。 The errors will automatically be available in your blade view in the $errors
variable.错误将自动显示在刀片视图中的
$errors
变量中。 (because of the withErrors() method) You can either make a listing of all errors: (由于 withErrors() 方法)您可以列出所有错误:
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
or you can check if a specific error is present: $errors->has('attrXY')
and display all the error messages for this attribute.或者您可以检查是否存在特定错误:
$errors->has('attrXY')
并显示此属性的所有错误消息。
Most of these things and even more useful information can be found in the documentation.这些内容中的大部分,甚至更多有用的信息都可以在文档中找到。 https://laravel.com/docs/8.x/validation#quick-displaying-the-validation-errors
https://laravel.com/docs/8.x/validation#quick-displaying-the-validation-errors
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.