3

我使用 laravel 的自定义验证不断收到此异常,但我不知道为什么。这是完整的异常消息:

local.ERROR: exception 'ErrorException' with message 'Argument 2 passed to Illuminate\Validation\Factory::make() must be of the type array, object given, called in /home/vagrant/Code/Spark/my-project/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 223 and defined' in /home/vagrant/Code/Spark/my-project/vendor/laravel/framework/src/Illuminate/Validation/Factory.php:91

这是我的代码:

$rules = array(
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
        'vat_id' => 'max:50|vat_id',
        'terms' => 'required|accepted',
        'address' => 'required|max:255',
        'city' => 'required|max:255',
        'state' => 'required',
        'contactName' => 'required',
        'phone' => 'numeric|required',
        'zip' => 'numeric|required',
    );

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

现在,我也这样尝试过,我得到了同样的例外:

    $validator = Validator::make($request->all(), [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
        'vat_id' => 'max:50|vat_id',
        'terms' => 'required|accepted',
        'address' => 'required|max:255',
        'city' => 'required|max:255',
        'state' => 'required',
        'contactName' => 'required',
        'phone' => 'numeric|required',
        'zip' => 'numeric|required',
]);

据我所知,我完全遵循文档(https://www.laravel.com/docs/5.2/validation#manually-creating-validators),并且正在传递一个数组。我在 laravel 中手动创建了很多次验证,但从未遇到过这个问题。我希望有一些明显的我做错了,也许另一双眼睛可以很容易地发现,因为这让我真的很难过。非常感谢任何帮助!如果它有所作为,我正在使用 Spark。

编辑:这是我在 var_dump $rules 时得到的:

array (size=12)
'name' => string 'required|max:255' (length=16)
'email' => string 'required|email|max:255|unique:users' (length=35)
'password' => string 'required|confirmed|min:6' (length=24)
'vat_id' => string 'max:50|vat_id' (length=13)
'terms' => string 'required|accepted' (length=17)
'address' => string 'required|max:255' (length=16)
'city' => string 'required|max:255' (length=16)
'state' => string 'required' (length=8)
'contactName' => string 'required' (length=8)
'phone' => string 'numeric|required' (length=16)
'zip' => string 'numeric|required' (length=16)
4

0 回答 0