问题描述
我目前正在尝试在我的第一个Laravel应用程序中验证创建文章"表单,但遇到了一些问题.我一直在关注适用于Laravel 5的教程,但是我正在该项目上运行Laravel 5.2.我已经阅读了Laravel 5.2中的Validation文档,并询问了另一个Laravel开发人员,但我们似乎无法弄清楚.
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
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()
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');
}
我要在哪里加载create.blade.php中的错误
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
从$ errors返回的ErrorBag完全为空,似乎不想填充.我也曾尝试使用Validator $ validator,但似乎无法加载它.
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.
任何帮助将不胜感激.
推荐答案
将所有路由放入其中以启用会话错误共享:
Put all your routes inside to enable Session errors sharing:
Route::group(['middleware' => ['web']], function () {
// Here comes your routes
});
更多详细信息:自定义请求表单验证成功时未调用控制器方法
这篇关于Laravel 5.2表单验证请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!