I'm using Laravel's form validation functionality and trying to work out how I can loop through the form errors in my view. At the moment, I'm successfully validating the form using the below code:
public function create()
{
$validator = Validator::make(
Input::all(),
array(
'username' => 'required|alpha_num|unique:users',
'email' => 'email|unique:users',
'password' => 'required|min:6',
'passwordConf' => 'required|same:password'
)
);
if ($validator->fails())
{
return Redirect::to('join')->withErrors($validator);
}
return View::make('user/join')->with();
}
The validator successfully validates the form and redirects to the join route if validation fails. Obviously, I'd also like to show the validation messages to the user. I have a master.blade.php layout file which all of my views extend, and in the layout I have the following code:
@if (Session::has('errors'))
<div class="alert alert-danger">
@foreach (Session::get('errors') as $error)
Test<br />
@endforeach
</div>
@endif
This seems to half work. If there are validation errors, the alert div does show on the page, however no validation errors get output. That suggests that Session::has('errors') is returning true however I'm obviously not iterating through the validation errors correctly.
How do I iterate through the validation errors sent to the view via withErrors?