0
votes

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

2
Try back()->withErrors(["error" => "test123"]); - Muthu17

2 Answers

0
votes

You need to return an array within withErrors()

0
votes

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