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



我创建了一个自定义验证规则

namespace AppRules;
use IlluminateContractsValidationRule;
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.';
}
}

但是当我尝试提交表单时,出现此错误

SymfonyComponentDebugExceptionFatalErrorException (E_UNKNOWN)
Declaration of AppRulesValidFoo::passes(string $attribute, $value): bool must be compatible with IlluminateContractsValidationRule::passes($attribute, $value)

这是拉拉维尔的规则界面

namespace IlluminateContractsValidation;
interface Rule
{
/**
* Determine if the validation rule passes.
*
* @param  string  $attribute
* @param  mixed  $value
* @return bool
*/
public function passes($attribute, $value);
/**
* Get the validation error message.
*
* @return string
*/
public function message();
}
namespace AppRules;
use IlluminateContractsValidationRule;
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.';
}
}

这是一个更正的类。 发生错误,因为您使用类型命中,但接口未使用类型提示

最新更新