Issues

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

Attached new version that supports defining the position in the parameter list where the value exists.

This doesn't appear to have been fixed in 1.5.0. Please update if this is not correct.

Please evaluate and categorize/assign as necessary.

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?

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.

My apologies. I just noticed that you did attach the implementation. :)

Just attached a slightly updated implementation.

What's the status of this issue? Seems like much wanted functionality, in a real old issue...

I'd love to see this added to the framework, but I don't have commit access.

Jordan, do you have a phpunit test for this based on the implementation?

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.

Working implementation within Incubator. Waiting for acceptance to core by dev-team.

Closing this issue as fixed for the next major release.