Laravel 5.2 - 不显示错误

时间:2016-01-13 17:53:33

标签: validation laravel laravel-5

无法在视图中显示错误

这是我的控制器:

public function create(){
        return view('articles.create');
    }

public function store(Request $request){
$this->validate($request, [
            'title' => 'required|max:5',
            'content' => 'required',
        ]);
}

这是我的观点create.blade.php:

        @if (count($errors) > 0)
        <!-- Form Error List -->
            <div class="alert alert-danger">
                <strong>Whoops!</strong> Something went wrong!.<br><br>
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

Kernel.php 已经存在ShareErrorsFromSession

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
           \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],

这是route.php 我在网上添加了路线

Route::group(['middleware' => ['web']], function () {
    Route::get('articles/create', 'ArticlesController@create'); // Display a form to create an article...
});
Route::get('articles', 'ArticlesController@index'); // Display all articles...
Route::post('articles', 'ArticlesController@store'); // Store a new article...
Route::get('articles/{id}', 'ArticlesController@show');

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情。

$data = array();
            $messages=array(
                'required'  =>  "You can't leave this empty",
            );
            $datavalidate = array(
                'name'      => $request->name,
            );  
            $rules = array(
                'name'              =>  'required',
            );

            $validator = Validator::make($datavalidate,$rules,$messages);
            if($validator->fails()){
                return Redirect::back()->withErrors($validator);
            }

现在将其打印在视图文件中。

{!! dd($errors) !!}