Details
-
Type:
New Feature
-
Status:
Resolved
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: 1.0.3
-
Fix Version/s: 1.10.0
-
Component/s: Zend_Validate
-
Labels:None
-
Fix Version Priority:Nice to Have
Description
This new validator provides the flexibility of not having to write a custom ZF validator for validation that can already be performed by another function.
class Zend_Validate_Callback extends Zend_Validate_Abstract { /** * @var string */ const INVALID_CALLBACK = 'invalidCallback'; /** * Message templates. * * @var array */ protected $_messageTemplates = array( self::INVALID_CALLBACK => "'%value%' is not valid" ); /** * The callback function/method. * * @var callback */ protected $_callback; /** * Additional parameters to send to the callback function/method. * * @var array */ protected $_params = array(); /** * Instantiates the callback filter. */ public function __construct($callback, array $params = array()) { $this->setCallback($callback)->_params = $params; } /** * Sets the callback function/method. * * @param callback $callback * * @return Zend_Validator_Callback */ public function setCallback($callback) { if (!is_callable($callback)) { throw new Zend_Filter_Exception('Invalid callback'); } $this->_callback = $callback; return $this; } /** * Validates the value using the callback function/method. * * @param string $value * * @return bool */ public function isValid($value) { $valueString = (string) $value; $this->_setValue($valueString); $params = array_merge(array($value), $this->_params); try { if (!call_user_func_array($this->_callback, $params)) { $this->_error(); return false; } } catch (Exception $e) { $this->_error(); return false; } return true; } }
Attached new version that supports defining the position in the parameter list where the value exists.