ZF-2268: Zend_Validate_Callback
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;
}
}
Comments
Posted by Jordan Ryan Moore (jordanryanmoore) on 2008-01-15T18:48:22.000+0000
Attached new version that supports defining the position in the parameter list where the value exists.
Posted by Wil Sinclair (wil) on 2008-04-18T13:11:56.000+0000
This doesn't appear to have been fixed in 1.5.0. Please update if this is not correct.
Posted by Wil Sinclair (wil) on 2008-04-18T16:55:02.000+0000
Please evaluate and categorize/assign as necessary.
Posted by Wil Sinclair (wil) on 2009-01-21T15:39:27.000+0000
I'm assuming that the main use case for this class is using validators from other frameworks/libraries. Is this correct? Are there any other targeted use cases? In any case, we think it's a good implementation and we'd like to include it in 1.8. Jordan, have you signed a CLA?
Posted by Wil Sinclair (wil) on 2009-01-26T09:16:27.000+0000
Assigning to Alex to implement. Alex, this change has been approved by the team. Reporter, you can coordinate with Alex to submit code/patches to implement this. It would be greatly appreciated.
Posted by Wil Sinclair (wil) on 2009-01-26T09:17:20.000+0000
My apologies. I just noticed that you did attach the implementation. :)
Posted by Jordan Ryan Moore (jordanryanmoore) on 2009-01-26T09:59:44.000+0000
Just attached a slightly updated implementation.
Posted by Dolf Schimmel (Freeaqingme) (freak) on 2009-06-29T19:36:43.000+0000
What's the status of this issue? Seems like much wanted functionality, in a real old issue...
Posted by Jordan Ryan Moore (jordanryanmoore) on 2009-06-29T20:00:24.000+0000
I'd love to see this added to the framework, but I don't have commit access.
Posted by Mike Willbanks (digitalstruct) on 2009-06-29T20:32:06.000+0000
Jordan, do you have a phpunit test for this based on the implementation?
Posted by Thomas Weidner (thomas) on 2009-06-30T00:24:51.000+0000
As you may have noted I added the necessary proposal several months ago. It is under recommendation at the dev-team since 29.April.2009 like 10 other proposals of mine.
As long as there is no acceptance from Zend I am not allowed to add anything to incubator. So we still have to wait.
Btw: I remember that the here available code has some problems which I already solved for Zend_Filter_Callback.
Posted by Thomas Weidner (thomas) on 2009-08-20T08:50:33.000+0000
Working implementation within Incubator. Waiting for acceptance to core by dev-team.
Closing this issue as fixed for the next major release.