Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$errors->has() is set to true when nothing has been submitted. #14615

Closed
adampatterson opened this issue Aug 3, 2016 · 5 comments
Closed

$errors->has() is set to true when nothing has been submitted. #14615

adampatterson opened this issue Aug 3, 2016 · 5 comments

Comments

@adampatterson
Copy link
Contributor

I would like to start off by saying that I think a lot of what laravel does is Magic, That in mind I don't have a very good understanding of how everything is related.

My project recently started showing a general validation error on page load Whoops! Something went wrong!. Nothing had been submitted yet. I went back through composer nad removed Sentry for Laravel as I had just updated from the depreciated Raven version.

Same issue.

Now I probably copied this code from an example that was not from Version 5.2 but it worked fine.

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

I am using ShareErrorsFromSession in my HTTPKernel.

When i was trying to debug this problem I used

{{ var_dump($errors->has()) }} <!-- true -->
{{ var_dump($errors->all()) }} <!-- none -->

I then went back in my code commits by weeks, and then months and still had the same issue.

I have since updated to use count($errors) > 0 and everything is fine. Validation validates with my messages and is no longer showing the error on page load.

But I started to wonder why this ended up happening.

$errors->has() was returning true even though nothing was set. Again I could be doing something wrong before but from version 5.2 and 5.1 mentioned using count() and version 5.0 didn't seem to touch that subject. I possibly found my solution here https://scotch.io/tutorials/laravel-form-validation#showing-errors-in-the-view But again this article is from 2014 but like is said, Everything worked and then it didn't.

Really long story short, Is the change made here:https://github.com/laravel/framework/blame/5.2/src/Illuminate/Support/MessageBag.php#L118 the cause?

My guess is that no matter how far back I went in my code, the only thing that would have changed would be the Laravel framework update.

Thanks!

@BrandonSurowiec
Copy link
Contributor

BrandonSurowiec commented Aug 3, 2016

You can also use @if($errors->count())

@adampatterson
Copy link
Contributor Author

@BrandonSurowiec Any insight as to why $errors->has() no longer works?

@BrandonSurowiec
Copy link
Contributor

@adampatterson It was broken due to some code refactoring in one of the last two releases, then was fixed in this commit: #14409

So the next release 5.2.42 should restore the expected behavior. You can view issue #14396 for more information.

@adampatterson
Copy link
Contributor Author

@BrandonSurowiec Thanks! I was looking my mind. Our site builds on Docker so when it updated composer on its creation it had a newer version than our local env. then when we tried to debug got the same error all the time.

Thanks again!

@adampatterson
Copy link
Contributor Author

ref #14396

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants