Laravel 中的验证错误 - 验证失败后未填充 $errors 数组 [英] Validation error in Laravel - $errors array does not get populated after the validation failure
问题描述
我在 Laravel 5.2 中遇到了一个关于验证的奇怪问题.我在 StackOverflow 上查看了以下问题,但似乎没有一个适用于我的情况:
问题是,在将 Card
对象持久化到数据库之前,我试图验证 title
字段.当我按预期提交带有空 title
字段的表单时,它没有通过验证.但是,$errors
数组不会在上述验证失败时填充.谁能解释一下这段代码我哪里出错了?
/////////////////////控制器/////////////////////公共函数创建(请求 $request){$this->validate($request, ['标题' =>'需要|分钟:10']);Card::create($request->all());返回();}
///////////////////////VIEW//////////////////////////显示错误,如果有的话.(永远不会被触发)@if(count($errors))<ul>@foreach($errors->all() as $error)<li>{{ $error }}</li>@endforeach@万一
<div class="form-group"><button class="btn btn-primary" type="submit">添加卡片</button>
</表单>
如果您运行的是 Laravel 5.2.27 及更高版本,则不再需要使用 Web 中间件组.事实上,您不应该将它添加到您的路由中,因为它现在默认会自动应用.
如果你打开你的 app/Http/RouteServiceProvider.php
文件,你会看到这段代码:
protected function mapWebRoutes(Router $router){$router->group(['命名空间' =>$this-> 命名空间,'中间件' =>'网络',], 函数 ($router) {require app_path('Http/routes.php');});}
来源:https://github.com/laravel/laravel/blob/master/app/Providers/RouteServiceProvider.php#L53
如您所见,它会自动为您应用网络中间件.如果您尝试在路由文件中再次(多次)应用它,您将遇到像您当前面临的问题一样的奇怪问题.
为了找出您正在运行的 Laravel 版本,请运行以下命令:php artisan --version
I've ran into a strange issue regarding validations in Laravel 5.2. I reviewed following questions on StackOverflow, but none of them seems to apply to my case:
Laravel validation not showing errors
Laravel Validation not returning error
The thing is, that I am trying to validate a title
field, before persisting the Card
object into the database. When I submit the form with an empty title
field, as expected, It doesn't pass the validations. However, the $errors
array doesn't get populated upon failure of the mentioned validations. Can anybody explain where am I going wrong with this code?
/////////////////////// CONTROLLER /////////////////////
public function create(Request $request)
{
$this->validate($request, [
'title' => 'required|min:10'
]);
Card::create($request->all());
return back();
}
///////////////////////// VIEW /////////////////////////
// Show errors, if any. (never gets triggered)
@if(count($errors))
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
<form method="POST" action="/cards">
{{ csrf_field() }}
<div class="form-group">
// The textarea does not get populated with the 'old' value as well
<textarea class="form-control" name="title">{{ old('title') }}</textarea>
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit">Add Card</button>
</div>
</form>
If you are running Laravel 5.2.27 and up, you no longer need to use the web middleware group. In fact, you shouldn't add it to your routes because it's now automatically applied by default.
If you open up your app/Http/RouteServiceProvider.php
file, you will see this bit of code:
protected function mapWebRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web',
], function ($router) {
require app_path('Http/routes.php');
});
}
Source: https://github.com/laravel/laravel/blob/master/app/Providers/RouteServiceProvider.php#L53
As you can see, it's automatically applying the web middleware for you. If you try to apply it again (more than once) in your routes file, you'll run into weird problems like what you are currently facing.
In order to find out the version of Laravel that you are running, run this command: php artisan --version
这篇关于Laravel 中的验证错误 - 验证失败后未填充 $errors 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!