Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SabrinaMarkon/8aa0ea5f412eac01429095203ca24177 to your computer and use it in GitHub Desktop.
Save SabrinaMarkon/8aa0ea5f412eac01429095203ca24177 to your computer and use it in GitHub Desktop.
Sabrina Notes - Laravel 5.2 - Validator: Things to check when you know there should be an error returned, but it is empty
Sabrina Notes - Laravel 5.2 - Validator: Things to check when you know there should be an error returned,
but it is empty. For instance, if you use Bootstrap, your error bar might show, but the words won't show
in it.
----------------------------------------------------------------------------------------------------------
1) My Mistake:
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach($errors as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
See for each validation error, we should get a nice list above the form that the user is filling out. Instead, there was
no error at all, and I could see that the error was COUNTED but the words (ie "fieldname is required") did not.
It appeared something wrong with Session::flash but was in fact simply the below! (that missing "->all()")
It should have @foreach($errors->all() as $error)
Trying to figure out this dumb mistake, also lead me to discover many other people that receive the same error,
but for a different reason, so I wanted to write it down, as I'm sure I'll run into it at some point on some site.
2) Do not include the web middleware in your routes.php if you are using Laravel 5.2.27 and newer, because the web middleware
is now included automatically. Recently enough (August 2016) that most Laravel tutorials still instruct to put the
web middleware in with your routes means that a lot of new people think it should go there. As well, people who are experienced
with Laravel encounter the problem because they are accustomed to previous RECENT versions still needing them to add it.
Apparently the duplication of web middleware registration results in potentially all sorts of peculiar behavior regarding
sessions (including flash errors and messages disappearing from session)
More information is here:
https://github.com/laravel/framework/issues/13000
http://laravel.io/forum/04-29-2016-validator-errors-not-in-session
I'm writing these notes because it is noteable that although 2) is a common cause for people to have problems with
validation errors being shown, many of them fixed the web middleware issue only to find their flash errors still didn't show.
And so if that happens, where the session "seems ok" and your validation code "seems ok" etc...check the code where the
error is displayed (my foreach block for example), and go over it carefully. It looks like nothing is wrong except the words
don't show with the mistake I made.
Sabrina Markon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment