Laravel 5.2 表单验证请求

Laravel 5.2 Form Validation Request

我目前正在尝试验证我的第一个 Laravel 申请中的 'Create Articles' 表单,但我遇到了一些问题。然而,我一直在关注针对 Laravel 5 的教程,并且我在这个项目中是 运行 Laravel 5.2。我已通读 Laravel 5.2 中的验证文档,并询问了另一位 Laravel 开发人员,但我们似乎无法弄清楚。

CreateArticleRequest.php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class CreateArticleRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
      * Get the validation rules that apply to the request.
      *
      * @return array
     */

    public function rules()
    {
        return [
            'title' => 'required|min:3',
            'body' => 'required',
            'published_at' => 'required|date'
        ];
    }
}

从 ArticlesController.php

创建()和存储()
public function create()
{
    return view('articles.create');
}

public function store(CreateArticleRequest $request)
{
    Article::create($request->all());

    return redirect('articles');
}

我试图加载 create.blade.php

中的错误
{{ dd($errors->all()) }}
@if ($errors->any())
    <ul class="alert alert-danger">
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
@endif

从 $errors 返回的 ErrorBag 完全是空的,似乎不想填充。我也尝试过使用验证器 $validator 但我似乎无法加载它。

如有任何帮助,我们将不胜感激。

将所有路由放入其中以启用会话错误共享:

Route::group(['middleware' => ['web']], function () {
    // Here comes your routes
});

更多详情:Custom request not calling controller method on form validation success