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.

图片转代码服务由CSDN问答提供 功能建议

我目前正在尝试在我的第一个Laravel应用程序中验证“创建文章”表单,我遇到了一些问题。 我一直在关注一个适用于Laravel 5的教程,但是,我在这个项目上运行Laravel 5.2。 我已经阅读了Laravel 5.2中的验证文档,我已经问了另一个Laravel开发人员,但我们似乎无法弄明白。

CreateArticleRequest.php

  namespace App \ Http \ Requests; 
 
use App \ Http \ Requests \ Request; \  n 
class CreateArticleRequest extends Request 
 {
 / ** 
 *确定用户是否有权发出此请求。
 * 
 * @return bool 
 * / 
 public function authorize()
  {
 return true; 
} 
 
 / ** 
 *获取适用于请求的验证规则。
 * 
 * @return array 
 * / 
 
公共函数规则(  )
 {
 return [
'title'=&gt;  'required | min:3',
'body'=&gt;  'required',
'manated_at'=&gt;  'required | date'
]; 
} 
} 
   
 
 

来自ArticlesController.php的create()和store() \ n

  public function create()
 {
返回视图('articles.create'); 
} 
 
公共函数存储(CreateArticleRequest $ request)
 {
 Article :: create  ($ request-&gt; all()); 
 
返回redirect('articles'); 
} 
   
 
 

我在哪里尝试加载 create.blade.php中的错误

  {{dd($ errors-&gt; all())}} 
 @ if($ errors-&gt; any())  
&lt; ul class =“alert alert-danger”&gt; 
 @foreach($ errors-&gt; all()as $ error)
&lt; li&gt; {{$ error}}&lt; / li&gt; \  n @endforeach 
&lt; / ul&gt; 
 @ endif 
   
 
 

从$ errors返回的ErrorBag完全为空,似乎不想 填充。 我也尝试使用Validator $验证器,但我似乎无法将其加载到。

任何帮助都将不胜感激。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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

    打赏 评论

相关推荐 更多相似问题