duannima8347 2016-02-12 12:02
浏览 89
已采纳

Laravel 5.2表单验证请求

I am currently trying to validate a 'Create Articles' form in my first Laravel application and I am having some problems. I have been following along with a tutorial that is meant for Laravel 5, however, and I am running Laravel 5.2 on this project. I have read through the documentation for Validation in Laravel 5.2 and I have asked another Laravel Developer but we just cannot seem to figure it out.

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

create() and store() from ArticlesController.php

public function create()
{
    return view('articles.create');
}

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

    return redirect('articles');
}

Where I am trying to load the errors in 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

The ErrorBag that is returned from $errors is completely empty and doesn't seem to want to populate. I have also tried to use the Validator $validator but I cannot seem to get that to even load in.

Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • douhuxi4145 2016-02-12 12:16
    关注

    Put all your routes inside to enable Session errors sharing:

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

    More details: Custom request not calling controller method on form validation success

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭