0

I am trying to validate an from with this code:

            $this->validate($request, [
            'address' => 'required',
            'quantity' => 'required',
        ],
        [
            'address.required' => 'Endereço Invalido',
            'quantity.required' => 'Quatidade Invalida'
        ]);

        return back()->withErrors("test123");

When I dont fill the form fields it returns to the form page with no errors, but when I filled it it return with the test123 error, what am I doing wrong on the validate here ? :\

Note: as people are responding me with soluctions to return back()->withErrors("test123"); my problem is not with the return, that is just a test that I use to know if the $errors array was beeing filled correctly to the blade template, my problem is with $this->validate() that is not passing the $error array to the blade template

Pedro Gaspar
  • 101
  • 1

2 Answers2

0

You need to return an array within withErrors()

z1haze
  • 431
  • 4
  • 7
  • my problem is not on return back()->withErrors("test123"); that was just a test to see if $errors got filled, my problem is with validate, that is working (as it returns if the fields are not filled) but is not backing with the proper errors – Pedro Gaspar Mar 27 '17 at 12:03
0

Validation errors are bound to views that have the Illuminate\View\Middleware\ShareErrorsFromSession middleware attached to them.

Could it be that your route isn't in the 'web' middleware group?

E.g.: Note the ->middleware('web');

Route::get('/', function () {
    //
})->middleware('web');

More reading here: https://laravel.com/docs/5.4/validation#quick-displaying-the-validation-errors

Mike Stivala
  • 111
  • 7
  • All my routes are on routes/web.php they should have the ShareErrorsFromSession on them, also the back()->withErrors("teste"); is working well – Pedro Gaspar Mar 27 '17 at 18:17
  • I've edited my answer to include a code example of what a route that has the 'web' middleware group looks like. – Mike Stivala Mar 27 '17 at 19:22