Laravel 5.2 Validation Error Not Appearing in Blade

Laravel 5.2 Validation Error not appearing in blade

Try to remove web middleware if you're using 5.2.27 or higher. The thing is now Laravel automatically applies web middleware to all routes inside routes.php and if you're trying to add it manually you can get errors.

app/Providers/RouteServiceProvider.php of the 5.2.27 version now adds web middleware to all routes inside routes.php:

protected function mapWebRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web',
], function ($router) {
require app_path('Http/routes.php');
});
}

Laravel 5.2 validation errors not appearing

There are a couple things wrong or that can be improved here. The store method on the UserController has a lot of weird issues. $this will always be true because objects are true in php. Also, you pass in $validator into withErrors which doesn't make sense because there's no variable validator.

public function store(Request $request) {
$this->validate($request, [
'email' => 'required|unique:users|email|max:255',
]);

User::create(Request::all());
return redirect('/');
}

The validate method will throw an Illuminate\Foundation\Validation\ValidationException if there is a validation error. This exception should be listed in the $dontReport instance variable in App\Exceptions\Handler as seen below:

protected $dontReport = [
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
ValidationException::class,
];

If you have changed these values, removed, or modified the ValidatesRequest trait you may have broken this functionality.

Your error reporting code is not correct either:

@foreach ($errors->all() as $error)
{!! $errors->first() !!}
@endforeach

There are 3 changes here. First I removed the outer errors size check, this doesn't really get you anything. Next, I fixed your extra } error, the syntax for un-escaping data is {!! $errors->first() !!}. Lastly, I called ->first() this returns the first error associated with that particular field.

I think it's important to note that the validation exception will create a redirect response to the previous page. The logic for determining the previous page can be found in Illuminate\Routing\UrlGenerator::previous().

Laravel 5.2 not showing form validation errors

This is a breaking problem with the 5.2 upgrade. What's happening is the middleware which is responsible for making that errors variable available to all your views is not being utilized because it was moved from the global middleware to the web middleware group.

There are two ways to fix this:

1-In your kernel.php file, you can move the middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class back to the protected $middleware property.

2-You can wrap all your web routes with a route group and apply the web middleware to them.

Route::group(['middleware' => 'web'], function() {
// Place all your web routes here...
});

See this

laravel-5-2-errors-not-appearing-in-blade

Laravel 5.2 - Validation errors are not getting displayed

If you're using Laravel version higher or equal to 5.2.25 you no longer need to put your routes in the web middleware. It is now automatically applied.

Putting your routes in the web middleware in the routes.php actually causes the problem.

Explained here.

Laravel 5.2 flash messages and errors suddenly not working

In order to access the $errors to my understanding you have to add it to the ErrorBag so instead you do this:

return redirect()->route('admin.events.create')->withErrors('not_found', $e->getMessage);

Its then you can access $errors variable and find it there in the view.

Update:
For those who might find this answer amongst the result of google search:

Based on these two answers: here and here and also the confirmation of OP:

moving the Illuminate\Session\Middleware\StartSession::class to $routeMiddleware was the solution according to the links and it worked.

I hope this is already fixed in Laravel I think so, since the web routes and others are now separated in L5.4.



Related Topics



Leave a reply



Submit