Laravel 5.2 Form Validation Request

2019-08-12 06:23发布

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条回答
Summer. ? 凉城
2楼-- · 2019-08-12 06:29

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

查看更多
登录 后发表回答