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

With proengsoft/laravel-jsvalidation I got " Argument 2 passed to must be of the type array" under Windows php7.4

我将 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",

谢谢!

确保使用 JsValidatorFacade 而不是 JsValidatorFactory

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

有时,您只需要运行遵循命令

php artisan config:cache

因为验证器没有加载刚刚在配置目录中创建的 jsvalidation 配置文件

希望对大家有所帮助。