[英]Phalcon PHP custom validation rule raise an error
我正在使用Phalcon框架,并且试图创建一个自定义验证器:
<?php
use Phalcon\Validation\Validator,
Phalcon\Validation\ValidatorInterface,
Phalcon\Validation\Message;
class Currency extends Validator implements ValidatorInterface
{
/**
* Executes the validation
*
* @param Phalcon\Validation $validator
* @param string $attribute
* @return boolean
*/
public function validate($validator, $attribute)
{
$value = $validator->getValue($attribute);
if(! is_numeric($value))
{
$message = $this->getOption('message');
if(! $message){
$message = 'Not a valid currency.';
}
$validator->appendMessage(new Message($message, $attribute, 'Currency'));
return false;
}
return true;
}
}
尝试使用上述验证器时出现此错误:
意外的值类型:实现Phalcon \\ Mvc \\ Model \\ ValidatorInterface的预期对象,给出了Currency类型的对象
我将验证器类放在/plugins/validators/Currency.php中,并且当然使用DI自动加载了它。 有什么线索吗?
这种验证器不能用于模型验证。 “ Phalcon \\ Validation是一个独立的验证组件,用于验证任意数据集。该组件可用于对不属于模型或集合的数据对象实施验证规则。”
此处提供更多信息: http : //docs.phalconphp.com/en/latest/reference/validation.html#validators
对于模型验证器,您需要使用另一个接口。
此处的信息:
http://docs.phalconphp.com/en/latest/reference/models.html#validating-data-integrity
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.