Laravel 5.2 validation errors

倾然丶 夕夏残阳落幕 提交于 2019-11-27 04:24:01

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.

Gowtham Selvaraj

Try using

return redirect()->back()
              ->withInput($request->all())
              ->withErrors($validator->errors()); // will return only the errors

Try to replace:

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

with:

->withErrors($validator);
// Replace

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

// with 

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

I have my working validation code in laravel 5.2 like this

first of all create a function in model like this

In model add this line of code at starting

use Illuminate\Support\Facades\Validator;

public static function validate($input) {

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

and in controller call this function to validate 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 comes the template part

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

and Above all the things you must write your Route like this

Route::group(['middleware' => ['web']], function () {

    Route::resource('RouteURL', 'ControllerName');
});

Wrap you Routes in web middleware like below:

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

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

//This will work

   Route::group(['middlewareGroups' => ['web']], function () {

// Add your routes here
});

//as well as this also works

Route::post('location',array(
'as'=>'location',
'middlewareGroups'=>'web',
'uses'=>'myController@function'
));
// Controller

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


// Blade Template

  @if ($errors->has('title'))
      <span class="error">
        <strong>{{ $errors->first('title') }}</strong>
      </span>
  @endif
  @if ($errors->has('anotherfied'))
      <span class="error">
        <strong>{{ $errors->first('anotherfied') }}</strong>
      </span>
  @endif

Find the documentation.

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


public function store(Request $request){
   $this->validate($request, [
       //input field names
      'title' => 'required|max:20',
      'body' => 'required',
   ]);
}

@if (count($errors) > 0)
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
@endif
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!