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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题