我创建了一个自定义验证器:
class MyValidator extends AbstractValidator
{
const ERROR_CONST = 'error';
protected $dbAdapter;
protected $messageTemplates = array(
self::ERROR_CONST => "Error msg for '%value%'."
);
public function __construct($dbAdapter)
{
$this->dbAdapter = $dbAdapter;
}
public function isValid($value, $context = null)
{
$this->setValue($value);
/**
* Do validation against db
*/
if(/* Not valid */){
$this->error(self::ERROR_CONST);
return false;
}
return true;
}
}
验证工作,我已经能够对其进行测试。不起作用的是使用以下命令输出错误消息
echo $this->formElementErrors($form->get('action'));
输出的只是一个空的UL。这是翻译问题吗?当我在验证器中的$ this-> getTranslator()上执行get_class时,我得到了验证器类名。当我var_dump $ this-> getTranslator()时,它输出为null。我是否需要为此设置翻译器,在哪里设置翻译器的最佳位置是我自己的验证器整个系统?
因为您__construct
为验证器类定义了一种方法,所以__construct
不会隐式调用父类:http : //php.net/manual/en/language.oop5.decon.php(请参阅注释)
您应该修改您的__construct
方法:
public function __construct($dbAdapter)
{
$this->dbAdapter = $dbAdapter;
//parent::__construct($options);
parent::__construct(null); // or (void)
}
如您所见,$messageTemplates
并$messageVariables
从中“加载”AbstractValidator::__construct
以用于某些方法(error
包括在内):
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句