Laravel 9验证器Make方法不起作用

sshcrbum  于 2023-01-03  发布在  其他
关注(0)|答案(1)|浏览(177)

我在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中调用
我该如何解决这个问题?

guykilcj

guykilcj1#

您只是遇到了一个简单的语法问题:

必须传递$request->all()数组,而不是$request instance

$validator = Validator::make($request->all(), $rules, $messages);

参考:https://laravel.com/docs/9.x/validation#manually-creating-validators

相关问题