Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 validation errors

Tags:

I have some trouble with validation in Laravel 5.2 When i try validate request in controller like this

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

Validator catch error, but don't store them to session, so when i'm try to call in template this code

 @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 throw the error

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

When i'm try validate 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');             } 

Variable $error also not available in template but if i try to display errors in controller

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

Errors displays but i can't access to them from template.

What's wrong?

like image 948
Dmitry Avatar asked Dec 22 '15 16:12

Dmitry


People also ask

How do you validate exact words in laravel?

To get the exact words to validate you can make use of Rule::in method available with laravel. Using Rule::in method whatever the values provided by this rule has to be matched otherwise it will fail.

How many types of validation are there in laravel?

Each form request generated by Laravel has two methods: authorize and rules .

What is the method used to configure validation rules in form request laravel?

Laravel Form Request class comes with two default methods auth() and rules() . You can perform any authorization logic in auth() method whether the current user is allowed to request or not. And in rules() method you can write all your validation rule.

What are the validations in laravel?

Validation is the most important aspect while designing an application. It validates the incoming data. By default, base controller class uses a ValidatesRequests trait which provides a convenient method to validate incoming HTTP requests with a variety of powerful validation rules.


2 Answers

Update as of Laravel 5.2.27

Laravel now supports the web middleware by default as you can see here: source

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

Below only applies to Laravel 5.2.0 to 5.2.26

Without seeing your routes.php or Kernel.php file, here is what I suspect is happening.

The way middlewares work has changed from 5.2 and 5.1. In 5.1, you will see this in your app/Http/Kernel.php file:

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 application's global HTTP middleware stack. In other words, they run on every request. Take a note at this particular middleware: Illuminate\View\Middleware\ShareErrorsFromSession. This is what adds the $errors variable on every request.

However, in 5.2, things have changed to allow for both a web UI and an API within the same application. Now, you will see this in that 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 maintenance. You now have a middleware group called "web" that includes a bulk of the previous global middleware. Remember that it is like this to allow for both a web UI and an API within the same application.

So how do we get that $errors variable back?

In your routes file, you need to add your routes within the "web" middleware group for you to have access to that $errors variable on every request. Like this:

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

If you aren't going to build an API, another option is to move the "web" middlewares to the global middleware stack like in 5.1.

like image 88
Thomas Kim Avatar answered Feb 13 '23 13:02

Thomas Kim


Try using

return redirect()->back()               ->withInput($request->all())               ->withErrors($validator->errors()); // will return only the errors 
like image 23
Gowtham Selvaraj Avatar answered Feb 13 '23 14:02

Gowtham Selvaraj