Laravel中的验证错误-验证失败后,不会填充$ errors数组 [英] Validation error in Laravel - $errors array does not get populated after the validation failure

查看:102
本文介绍了Laravel中的验证错误-验证失败后,不会填充$ errors数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Laravel 5.2中的验证,我遇到了一个奇怪的问题.我查看了关于StackOverflow的以下问题,但似乎没有一个适用于我的情况:

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验证未显示错误

Laravel验证未返回错误

问题是,在将Card对象持久保存到数据库之前,我正在尝试验证title字段.当我按预期提交带有空title字段的表单时,它没有通过验证.但是,在提到的验证失败时,不会填充$errors数组.有人可以解释这段代码在哪里出问题吗?

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>

推荐答案

如果您正在运行Laravel 5.2.27及更高版本,则不再需要使用Web中间件组.实际上,您不应该将其添加到您的路线中,因为默认情况下它现在会自动应用.

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.

如果打开app/Http/RouteServiceProvider.php文件,您将看到以下代码:

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');
    });
}

来源: https://github.com/laravel/laravel/blob /master/app/Providers/RouteServiceProvider.php#L53

如您所见,它会自动为您应用Web中间件.如果您尝试在路径文件中再次(不止一次)应用它,则会遇到诸如当前所面临的问题之类的奇怪问题.

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.

为了找出您正在运行的Laravel版本,请运行以下命令:php artisan --version

In order to find out the version of Laravel that you are running, run this command: php artisan --version

这篇关于Laravel中的验证错误-验证失败后,不会填充$ errors数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆