[英]Laravel 9 Validator Make Method Not Seems To Be Working
I have written this code in the Controller as the Action of a form:我在 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();
}
...
}
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
Illuminate\Validation\Factory::make(): 参数 #1 ($data) 必须是数组类型,Illuminate\Http\Request given, called in C:\projectname\vendor\laravel\framework\src\Illuminate\Support\ Facades\Facade.php 第 338 行
So what's going wrong here?那么这里出了什么问题?
How can I solve this issue?我该如何解决这个问题?
You just have a simple syntax issue:你只是有一个简单的语法问题:
You must pass $request->all()
array not the $request instance
您必须传递
$request->all()
数组而不是$request instance
$validator = Validator::make($request->all(), $rules, $messages);
Reference : https://laravel.com/docs/9.x/validation#manually-creating-validators参考: https://laravel.com/docs/9.x/validation#manually-creating-validators
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.