Search code examples
laravellaravel-bladelaravel-validationlaravel-form

Error using withErrors() when returning view


I would like to submit my laravel form, with some errors if my csv is not set to the input file. So I did like this, simple, basic :

public function store(Request $request) {

    $validator = Validator::make($request->all(), [
        'csv' => 'required|mimes:csv',
    ]);

    if($validator->fails())
    {
        return view(backpack_view('upload/upload-nb-postes'))->withErrors($validator);
    }
}

The validator fails, of course, so it should render my view with error messages but I have an error :

Facade\Ignition\Exceptions\ViewException
Call to undefined method Illuminate\Support\MessageBag::getBag() 

I checked the MessageBag class and ... yes, there is no getBag() method. So what should I do ? I can't use the withErrors() method...

I tried with :

return view(backpack_view('upload/upload-nb-postes'))->withErrors(['csv' = > 'test');

Same problem, I also checked that there is some error messages.

Maybe update with composer ? I won't crash my project.

More information :

Laravel version : 6.17.1
Laravel locale : fr
Laravel config cached : false
PHP version : 7.4.3

Solution

  • You have to call errors method of $validator instance.

    return view(backpack_view('upload/upload-nb-postes'))->withErrors($validator->errors());
    

    Inside your view, use $errors->first('csv') to access error message.