Laravel 5.2 validation errors

I have some validation problems in Laravel 5.2 when I try to validate a request in a controller like this

$this->validate($request, [ 'title' => 'required', 'content.*.rate' => 'required', ]); 

Validator validation error, but do not store them in the session, so when I try to call this code in the template

  @if (isset($errors) && count($errors) > 0) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif 

Laravel gives an error

 Undefined variable: errors (View: /home/vagrant/Code/os.dev/resources/views/semantic/index.blade.php) 

When I try to check with this code

  $validator = Validator::make($request->all(), [ 'title' => 'required', 'content.*.rate' => 'required' ]); if ($validator->fails()) { return redirect() ->back() ->withInput($request->all()) ->withErrors($validator, 'error'); } 

The $ error variable is also not available in the template, but if I try to display errors in the controller

  if ($validator->fails()) { dd($validator->errors()->all()); } 

Errors are displayed, but I cannot access them from the template.

What's wrong?

+21
laravel laravel-validation
Dec 22 '15 at 16:44
source share
7 answers

Update from Laravel 5.2.27

Laravel now supports network middleware by default, as you can here: source

In other words, you no longer need to wrap routes around the middleware group because it does this for you in the RouteServiceProvider file. However, if you are using the Laravel version between 5.2.0 and 5.2.26, refer to the following method:

The following applies only to Laravel 5.2.0 to 5.2.26

Without seeing your routes.php or Kernel.php , I suspect this is happening.

The mode of operation of middlewares has changed from 5.2 and 5.1. In 5.1, you will see this in your app/Http/Kernel.php :

 protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, ]; 

This array is your global HTTP middleware stack. In other words, they run in every request. Pay attention to this middleware: Illuminate\View\Middleware\ShareErrorsFromSession . This is what adds the $errors variable for each request.

However, in 5.2, everything changed to use both the web interface and the API in one application. Now you will see this in the same file:

 protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, ]; protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, ], 'api' => [ 'throttle:60,1', ], ]; 

The global middleware stack now only checks for service. You now have an intermediate group called "web" that includes most of the previous global middleware. Remember that this is the way to enable both the web interface and the API in one application.

So how can we return this $errors variable?

In your routes file, you need to add your routes to the middleware group to access this $errors variable for each request. Like this:

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

If you are not going to create an API, another option is to move the web intermediaries to the global middleware stack, as in 5.1.

+50
Dec 22 '15 at 17:28
source share

Try using

 return redirect()->back() ->withInput($request->all()) ->withErrors($validator->errors()); // will return only the errors 
+4
Apr 30 '16 at 8:15
source share

Try replacing:

 ->withErrors($validator, 'error'); 

from:

 ->withErrors($validator); 
+3
Dec 22 '15 at 17:07
source share
 // Replace Route::group(['middleware' => ['web']], function () { // Add your routes here }); // with Route::group(['middlewareGroups' => ['web']], function () { // Add your routes here }); 
+2
May 30 '16 at 19:10
source share

I have my health check code in laravel 5.2 like this

first create a function in a model like this

In the model, add this line of code at startup

use Illuminate \ Support \ Facades \ Validator;

 public static function validate($input) { $rules = array( 'title' => 'required', 'content.*.rate' => 'required', ); return Validator::make($input, $rules); } 

and in the controller call this function to check the input

use Illuminate \ Support \ Facades \ Redirect;

  $validate = ModelName::validate($inputs); if ($validate->passes()) { ///some code }else{ return Redirect::to('Route/URL') ->withErrors($validate) ->withInput(); } 

Now here is part of the template

 @if (count($errors) > 0) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif 

and, first of all, you should write your route this way

 Route::group(['middleware' => ['web']], function () { Route::resource('RouteURL', 'ControllerName'); }); 
0
May 05 '16 at 9:43 a.m.
source share

Wrap your routes in web middleware as shown below:

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

and In app\Http\Kernel.php move \Illuminate\Session\Middleware\StartSession::class from web $middlewareGroups to $middleware hope this solves your problem.

0
May 17 '16 at 4:07
source share

// This will work

  Route::group(['middlewareGroups' => ['web']], function () { // Add your routes here }); 

// also works

 Route::post('location',array( 'as'=>'location', 'middlewareGroups'=>'web', 'uses'=>'myController@function' )); 
0
Jun 14 '16 at 10:16
source share



All Articles