Sanzeeb Aryal Sanzeeb Aryal - 6 months ago 12
PHP Question

Laravel validator only displaying message after reloading

I am trying to validate the image using validator

$validator=Validator::make($request->all(),[
'image'=>'image',
]);

if($validator->fails()){
return redirect('/pop/add')
->withErrors($validator);
}


But when i add the file except the image it is displaying
MethodNotFound Exception
instead of the error message and when i reload the page it then displays the validation error message.

The view looks like:

<form method="POST" action="{{url('/pop/add')}}" role="form" enctype="multipart/form-data">
{!! csrf_field() !!}
<label>Upload your photo:</label><input type="file" name="image" ><br>


and the route:

Route::get('/pop/addview','popcontroller@addview');
Route::post('/pop/add','popcontroller@addnow');

Answer

You should redirect to '/pop/addview' instead of '/pop/add'.

return redirect('/pop/addview')
    ->withErrors($validator);