Zend Framework

Zend_Validate_Callback

Details

  • Type: New Feature New Feature
  • Status: Resolved Resolved
  • Priority: Minor 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;
    }
}
  1. Callback.php
    26/Jan/09 9:59 AM
    4 kB
    Jordan Ryan Moore
  2. Callback.php
    15/Jan/08 6:48 PM
    3 kB
    Jordan Ryan Moore

Activity

Hide
Jordan Ryan Moore added a comment -

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

Show
Jordan Ryan Moore added a comment - Attached new version that supports defining the position in the parameter list where the value exists.
Hide
Wil Sinclair added a comment -

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

Show
Wil Sinclair added a comment - This doesn't appear to have been fixed in 1.5.0. Please update if this is not correct.
Hide
Wil Sinclair added a comment -

Please evaluate and categorize/assign as necessary.

Show
Wil Sinclair added a comment - Please evaluate and categorize/assign as necessary.
Hide
Wil Sinclair added a comment -

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?

Show
Wil Sinclair added a comment - 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?
Hide
Wil Sinclair added a comment -

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.

Show
Wil Sinclair added a comment - 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.
Hide
Wil Sinclair added a comment -

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

Show
Wil Sinclair added a comment - My apologies. I just noticed that you did attach the implementation.
Hide
Jordan Ryan Moore added a comment -

Just attached a slightly updated implementation.

Show
Jordan Ryan Moore added a comment - Just attached a slightly updated implementation.
Hide
Dolf Schimmel (Freeaqingme) added a comment -

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

Show
Dolf Schimmel (Freeaqingme) added a comment - What's the status of this issue? Seems like much wanted functionality, in a real old issue...
Hide
Jordan Ryan Moore added a comment -

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

Show
Jordan Ryan Moore added a comment - I'd love to see this added to the framework, but I don't have commit access.
Hide
Mike Willbanks added a comment -

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

Show
Mike Willbanks added a comment - Jordan, do you have a phpunit test for this based on the implementation?
Hide
Thomas Weidner added a comment -

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.

Show
Thomas Weidner added a comment - 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.
Hide
Thomas Weidner added a comment -

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

Closing this issue as fixed for the next major release.

Show
Thomas Weidner added a comment - Working implementation within Incubator. Waiting for acceptance to core by dev-team. Closing this issue as fixed for the next major release.

People

Vote (5)
Watch (4)

Dates

  • Created:
    Updated:
    Resolved: