How to Resolve Laravel Validation Errors Not Displaying
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
⇨ Click here for the table of contents for Laravel articles
In this article,
Laravel validation errors not displayed in view
will be the topic of resolution.
Previous article: How to Validate in Laravel Form Requests
Environment
MacOS
Laravel6
Solution
There may be a possibility that the errors are not passed through the errors middleware.
App/Http/Kernel.php
Here, specify the middleware to use,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
If this class is not included, errors will not be displayed.
It can be seen that this class is attached to the default "web" middleware group.
App/Http/Kernel.php
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
]
If this class is not attached for some reason (e.g., creating a custom middleware group), errors will not be displayed.
Sample Code
There are many reasons why validation may not be displayed, but I have provided one example, so please refer to the following article to see if the basic validation method is incorrect.
⇨ [Beginner-friendly] The Easiest Way to Validate in Laravel
⇨ [Beginner-friendly] Validating in Laravel with Form Requests
Conclusion
In this article, I discussed how to handle cases where errors are not displayed even though the Laravel validation is written correctly.
Frameworks are convenient but can be difficult to fix if you deviate from the initial setup!
For complaints or pointing out mistakes in the article, please DM me on Twitter.
Article frequently read: Implementing Email Verification in Laravel6 Auth (VerifyEmail)