首页 > 解决方案 > 使用 proengsoft/laravel-jsvalidation 我在 Windows php7.4 下得到“传递给的参数 2 必须是数组类型”

问题描述

我将 proengsoft/laravel-jsvalidation 添加到我的 laravel 6 应用程序中,该应用程序在我的 ubuntu 18 上使用 PHP 7.2.24 可以正常工作,但是我的客户端拉取版本在他的 Windows / php 7.4.2 上出现错误,如下所示:

#57 C:\\xampp\\htdocs\\primewatch\\public\\index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#58 {main}

[previous exception] [object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Argument 2 passed to Proengsoft\\JsValidation\\JsValidatorFactory::__construct() must be of the type array, null given, called in C:\\xampp\\htdocs\\primewatch\\vendor\\proengsoft\\laravel-jsvalidation\\src\\JsValidationServiceProvider.php on line 38 at C:\\xampp\\htdocs\\primewatch\\vendor\\proengsoft\\laravel-jsvalidation\\src\\JsValidatorFactory.php:37)
[stacktrace]
#0 C:\\xampp\\htdocs\\primewatch\\vendor\\proengsoft\\laravel-jsvalidation\\src\\JsValidationServiceProvider.php(38): Proengsoft\\JsValidation\\JsValidatorFactory->__construct(Object(Illuminate\\Foundation\\Application), NULL)
#1 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(799): Proengsoft\\JsValidation\\JsValidationServiceProvider->Proengsoft\\JsValidation\\{closure}(Object(Illuminate\\Foundation\\Application), Array)
#2 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(681): Illuminate\\Container\\Container->build(Object(Closure))
#3 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php(785): Illuminate\\Container\\Container->resolve('jsvalidator', Array, true)
#4 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(629): Illuminate\\Foundation\\Application->resolve('jsvalidator', Array)
#5 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php(770): Illuminate\\Container\\Container->make('jsvalidator', Array)
#6 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(1245): Illuminate\\Foundation\\Application->make('jsvalidator')
#7 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Facades\\Facade.php(198): Illuminate\\Container\\Container->offsetGet('jsvalidator')
#8 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Facades\\Facade.php(166): Illuminate\\Support\\Facades\\Facade::resolveFacadeInstance('jsvalidator')
#9 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Facades\\Facade.php(255): Illuminate\\Support\\Facades\\Facade::getFacadeRoot()
#10 C:\\xampp\\htdocs\\primewatch\\storage\\framework\\views\\68757a9c9b2f658eb9358cc98549740ca7f9e23d.php(34): Illuminate\\Support\\Facades\\Facade::__callStatic('formRequest', Array)
#11 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Engines\\PhpEngine.php(43): include('C:\\\\xampp\\\\htdocs...')
#12 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Engines\\CompilerEngine.php(59): Illuminate\\View\\Engines\\PhpEngine->evaluatePath('C:\\\\xampp\\\\htdocs...', Array)
#13 C:\\xampp\\htdocs\\primewatch\\vendor\\facade\\ignition\\src\\Views\\Engines\\CompilerEngine.php(36): Illuminate\\View\\Engines\\CompilerEngine->get('C:\\\\xampp\\\\htdocs...', Array)
#14 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\View\\View.php(143): Facade\\Ignition\\Views\\Engines\\CompilerEngine->get('C:\\\\xampp\\\\htdocs...', Array)

我有 app/Http/Requests/ShipRequest.php :

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use App\Http\Traits\funcsTrait;

use App\Ship;

class ShipRequest extends FormRequest
{
    use funcsTrait;
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $request= Request();
        return Ship::getValidationRulesArray( $request->get('id') );
    }

}

在 Ship Model 方法中,getValidationRulesArray 定义为:

public static function getValidationRulesArray($ship_id= null) : array
{
    $validationRulesArray = [
        'title' => [
            'required',
            'string',
            'max:255',
            Rule::unique(with(new Ship)->getTable())->ignore($ship_id),
        ],
        'company_id'      => 'required|exists:' . ( with(new Company)->getTable() ).',id',
    ];
    return $validationRulesArray;
}

我想知道为什么在 Windows/php 7.4 上会出现这样的错误。可能是php不同的问题还是什么?

"laravel/framework": "^6.2",
"proengsoft/laravel-jsvalidation": "^2.5",

谢谢!

标签: laravellaravel-validation

解决方案


确保使用 JsValidatorFacade 而不是 JsValidatorFactory

你可以使用这个语法使用 Proengsoft\JsValidation\Facades\JsValidatorFacade 作为 JsValidator;


推荐阅读