【问题标题】:Laravel 5.2 Form Validation RequestLaravel 5.2 表单验证请求
【发布时间】:2016-05-23 13:49:33
【问题描述】:

我目前正在尝试在我的第一个 Laravel 应用程序中验证“创建文章”表单,但遇到了一些问题。但是,我一直在关注适用于 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 的 create() 和 store()

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 $validator 但我似乎无法让它加载。

任何帮助将不胜感激。

【问题讨论】:

    标签: php validation laravel laravel-5 laravel-5.2


    【解决方案1】:

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

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

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

    【讨论】:

    • 这成功了!我只需要将我的 ::post 路由移动到组中,现在一切正常。
    猜你喜欢
    • 2016-09-06
    • 2017-03-11
    • 2016-09-02
    • 2016-03-11
    • 2015-06-09
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    相关资源
    最近更新 更多