传递声明(字符串$ attribute,$ value):bool必须与Illuminate \ Contracts \ Validation \ Rule :: passes($ attribute,$ value)兼容

时间:2018-05-31 21:03:49

标签: laravel laravel-5 laravel-5.5

我已经创建了自定义验证规则

let fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
let select = [0, 1, 1, 1, 0];
let result = fruits.filter((fruit, i) => select[i]);

console.log(result);

但是,当我尝试提交表单时,我收到此错误

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Exception;

class ValidFoo implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes(string $attribute, $value): bool
    {
        if (!$foo) {
            return false;
        }

        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message(): string
    {
        return 'The foo you \'ve provided is not valid.';
    }
}

这是Laravel的规则界面

Symfony\Component\Debug\Exception\FatalErrorException (E_UNKNOWN)
Declaration of App\Rules\ValidFoo::passes(string $attribute, $value): bool must be compatible with Illuminate\Contracts\Validation\Rule::passes($attribute, $value)

1 个答案:

答案 0 :(得分:1)

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Exception;

class ValidFoo implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        if (!$foo) {
            return false;
        }

        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message(): string
    {
        return 'The foo you \'ve provided is not valid.';
    }
}

这是一个更正的课程。出现错误,因为您使用了类型匹配,但接口未使用类型提示