How to pass converted value of field for a specific validator in Zend Framework?
Suppose I have a field for IP address. It has 2 validators - Ip - Db_NoRecordsExists
$ip = new Zend_Form_Element_Text('ip');
$ip->setLabel('IP')
->setRequired(true)
->addValidator('Ip')
->addValidator('Db_NoRecordExists', false, array('table' => 'blacklist_ips', 'field' => 'ip'));
In DB ip stored as integer, so I have a problem with validation on unique, using Db_NoRecordsExists.
Is it possible to pass ip as converted to integer, but only for one of validator (because in case of converting field value before validate, Ip validator will gives error), something like this (added new parameter "value"):
$ip->setLabel('IP')
->setRequired(true)
->addValidator('Ip')
->addValidator('Db_NoRecordExists', false, array('table' => 'blacklist_ips', 'field' =>开发者_如何学运维; 'ip', 'value' => Custom_Convert_Ip::ip2long([value_of_ip])));
Thanks in advance.
One way would be to write your own validator Zend_Validate_Db_NoRecordExists
. For example:
class My_Validate_Db_NoRecordExists extends Zend_Validate_Db_NoRecordExists {
public function isValid($value)
{
return parent::isValid(new Zend_Db_Expr("INET_ATON('$value')"));
}
}
I haven't tested it, but it I think it should do the trick.
Hope it helps.
精彩评论