Friday
18
September
2009

Zend_Validate_Callback

I just added a new component to Zend Framework which will be available to public as with release 1.10.

Zend_Validate_Callback:
Several components within Zend Framework use Validators to check data or input.
This validator can be used to add a self defined method as validator.

How does this work:

$valid = new Zend_Validate_Callback('is_int');
	
$valid->isValid(123); // returns true
$valid->isValid('test'); // returns false

Of course this is only a very small example…
You can use self written functions or class methods as validators.

PHP5.3 makes live even easier… you can also use Closures or Invokes. See this example for closures:

$valid = new Zend_Validate_Callback(
    function ($value) {
        if (is_int($value)) {
            return true;
        }
        return false;
    }
);

This can be very handy for small self written functions.
Using this validator you should be aware of some rules:

* A validator should always return true or false
* It should not throw an exception
* When possible use native php methods

I hope you find this validator usefull and have use for it.

More to come soon

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top