Laravel - 表单验证错误 - 参数 2 必须是数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33135806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:30:24  来源:igfitidea点击:

Laravel - Form validation error - argument 2 must be array

phpformsvalidationlaravel

提问by Anhinga

I'm working with Laravel and every time I submit my form it gives me this error:

我正在使用 Laravel,每次提交表单时都会出现以下错误:

ErrorException in Factory.php line 91: Argument 2 passed to Illuminate\Validation\Factory::make() must be of the type array, null given, called in /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php on line 83 and defined

Factory.php 第 91 行中的 ErrorException:传递给 Illuminate\Validation\Factory::make() 的参数 2 必须是数组类型,给定 null,在 /var/www/vendor/laravel/framework/src/Illuminate/Foundation 中调用/Http/FormRequest.php 在第 83 行并定义

This is some code for the controller, even when I don't try to send data to the database it gives me this error. (now it's just redirecting)

这是控制器的一些代码,即使我不尝试将数据发送到数据库,它也会给我这个错误。(现在只是重定向)

public function store(StoreProjectRequest $request)
{


    return Redirect::to('/index');

}

This is how I defined my routes:

这就是我定义路线的方式:

Route::get('/projects','ProjectsController@index');
Route::get('/create','ProjectsController@create');

Route::post('/create','ProjectsController@store');

The line the error refers to is what is in the return section here:

错误所指的行是这里返回部分中的内容:

protected function getValidatorInstance()
{
    $factory = $this->container->make('Illuminate\Validation\Factory');

    if (method_exists($this, 'validator')) {
        return $this->container->call([$this, 'validator'], compact('factory'));
    }

    return $factory->make(
        $this->all(), $this->container->call([$this, 'rules']), $this->messages(), $this->attributes()
    );
}

Can anyone help me? Thank you!

谁能帮我?谢谢!

回答by Maxim Lanin

Problem is in your StoreProjectRequestand it's rules()method. It should return array and in your code it probably returns something else. Check it, please.

问题出在你StoreProjectRequestrules()方法上。它应该返回数组,并且在您的代码中它可能返回其他内容。请检查一下。