使用请求不工作的laravel 5验证 - 未定义的变量错误

时间:2016-01-05 14:27:33

标签: php forms validation laravel-5 request

我正在使用Laravel 5.2,我有表单并希望插入带验证的字段。我在Requests文件夹下创建了createQuranRequest并尝试回显验证错误,但我得到了以下错误

  

a19890dff92858726bf2b1048815af329d53d3b6.php第6行中的ErrorException:   未定义的变量:错误(查看:   /Applications/MAMP/htdocs/quran/resources/views/pages/quranForm.blade.php)   (查看:/应用程序/ MAMP / htdocs中/古兰经/资源/视图/页   /quranForm.blade.php)

我的quranForm.blade.php代码我试图吐错误

<div class="form-group">
  {!!Form::label('title','Surah Title:')!!}    
  {!!Form::text('title',null,['class' => 'form-control'])!!} 
  <span class="help-block">{{$errors->first('title') }}</span>
 </div>

 <div class="form-group">
   {!!Form::label('lyrics','Surah Lyrics:')!!}       
   {!!Form::textarea('lyrics',null,['class' => 'form-control'])!!}  
 </div>

 <div class="form-group">
   {!!Form::submit('Add Surah',['class' => 'btn btn-primary'])!!}
 </div>

我的createQuranRequest文件

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class createQuranRequest 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',
            'lyrics' => 'required'
        ];
    }
}

我的控制器文件

public function store(createQuranRequest $request, quran $quran){ 
  $quran->create($request->all());
  return redirect('quran');
}

我试过下面的代码

@if (Session::get('errors'))
  <ul>
    @foreach ($errors->all() as $error)
      <li>{ { $error } }</li>
    @endforeach
  </ul>
@endif   

它删除了异常错误,但未显示错误。

1 个答案:

答案 0 :(得分:1)

laravel文档说,$ errors变量由Illuminate \ View \ Middleware \ ShareErrorsFromSession中间件绑定到视图,该中间件由Web中间件组提供。

所以你必须更新app / http / routes.php

中的路线文件
Route::group(['middleware' => ['web']], function () 
{
    //this route will use the middleware of the 'web' group, so session and auth will work here         
    Route::post('/your-endpoint','MyController@store');       
});