bosnadev

№ 10 · 2014-12-30 · 1 min · pro-tips

Pro tip: Display all validation errors in Blade View

In Laravel 4, errors passed with withErrors() are available in every Blade view as $errors. A short loop displays all of them at once.

written before the past year · entry 10 of 31 · 10 in the past year

This article is out of date
Written for Laravel 4 in 2014.

Every Blade View have $errors variable bounded to it unless you set different one by yourself. If you use controller based error handling, you will probably have something like this inside it:

if ($validator->fails()){   return Redirect::to('/profile')    ->withErrors($validator)    ->withInput();}

We passed validation errors to the profile view using withErrors() method and inside that view you can display them:

@if($errors->has())   @foreach ($errors->all() as $error)      <div>{{ $error }}</div>  @endforeach@endif

Related

Pro tip: How to repair an Ubuntu system with a Live CDpro-tipsAwesome Looking Terminal With Oh-My-Zshpro-tipsLaravel Validation: Comprehensive Guide, part 1laravel