I have written this code in the Controller as the Action of a form:
public function submitAsk(Request $request)
{
$rules = [
'title' => 'required|max:255',
'description' => 'required|max:1000',
'category' => 'required',
'tags' => 'required',
];
$messages = [
'required' => ':attribute can not be empty'
];
$validator = Validator::make($request, $rules, $messages);
if ($validator->fails()) {
return redirect('questions/ask')
->withErrors($validator)
->withInput();
}
...
}
But I get this error:
Illuminate\Validation\Factory::make(): Argument #1 ($data) must be of type array, Illuminate\Http\Request given, called in C:\projectname\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php on line 338
How can I solve this issue?
CodePudding user response:
You just have a simple syntax issue:
You must pass $request->all()
array not the $request instance
$validator = Validator::make($request->all(), $rules, $messages);
Reference: https://laravel.com/docs/9.x/validation#manually-creating-validators