Thanks for your input!
We actually talked about doing that (custom validators) soon after we
realized that the isValid() methods were seemingly build *for* validators.
Trying to not repeat ourselves by creating custom validators left-and-right,
I came up with a neat callback one...
<?php
class My_Validate_Callback extends Zend_Validate_Abstract
{
const ERROR_KEY = 'message';
private $_callback;
private $_params = array();
protected $_messageTemplates = array();
public function __construct($callback, $params = array(), $message)
{
$this->_callback = $callback;
$this->_params = $params;
$this->_messageTemplates = array(self::ERROR_KEY => $message);
}
public function isValid($value)
{
$params = $this->_params;
$params[] = $value;
if (call_user_func_array($this->_callback, $params)) {
return true;
} else {
$this->_error(self::ERROR_KEY);
return false;
}
}
}
?>
Usage
<?php
$email = new Zend_Form_Element_Text('email');
$email->addValidator(new My_Callback_Validator(array('User',
'isEmailUnique'), array(), 'Email is not unique');
?>
The 2nd argument allows you to pass more data to the callback method
<?php
new My_Callback_Validator(array('User', 'isEmailUnique'), array(), 'Email is
not unique')
Method sig: User::isEmailUnique($value);
new My_Callback_Validator(array('User', 'isEmailUnique'), array('firstVar',
'secondVar'), 'Email is not unique')
Method sig: User::isEmailUnique($first, $second, $value);
--
View this message in context:
http://n4.nabble.com/Overriding-Zend-Form-isValid-tp1018622p1018652.html
Sent from the Zend Framework mailing list archive at Nabble.com.