我在Controller中编写了以下代码作为表单的Action:
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();
}
...
}
但我得到这个错误:
照明\验证\工厂::make():参数#1($data)必须是数组类型,给定Illuminate\Http\Request,在第338行的C:\项目名称\供应商\laravel\框架\src\Illuminate\支持\Facades\Facade.php中调用
我该如何解决这个问题?
1条答案
按热度按时间guykilcj1#
您只是遇到了一个简单的语法问题:
必须传递
$request->all()
数组,而不是$request instance
参考:https://laravel.com/docs/9.x/validation#manually-creating-validators