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
  1. Tobi

    Saturday, September 19, 2009 - 13:04:49

    Cool idea, but I think it only makes sense with PHP 5.3 and lambda functions! The other way seems a little bit ‘old style’.

    Best regards
    Tobi

  2. thomasw

    Saturday, September 19, 2009 - 14:43:18

    I would not call it “old style” to be able to use self defined methods as validator.

    When you know a better way to use self defined methods as validator without building them as validator, which means to implement the abstract validator or the interface, and to provide a own path so it will be found, feel free to propose it.

    Callback is still the fastest and simplest way for such a usecase. ;-)

  3. thomasw

    Saturday, September 19, 2009 - 22:45:15

    I would ask all readers to accept the netiquete and write in english and not german, russian or any other language.

    All non-english comments will be erased.

  4. derk

    Thursday, September 24, 2009 - 21:14:22

    if (is_int($value)) {
    return true;
    }
    return false;

    Shorter version:
    return is_int($value);

  5. Yudi Lee

    Friday, October 2, 2009 - 08:11:17

    Can we use zend_validate_callback on zend_validate->addValidator() ?
    Can someone show me how to do that?

  6. thomasw

    Saturday, October 3, 2009 - 11:38:27

    Of course…. look into the manual to see how parameters have to be given to addValidator().

  7. Giorgio Sironi

    Saturday, December 19, 2009 - 14:06:00

    I am using this validator by manual download (since it has no external dependencies). Is there a way to change the error message template via subclassing or options? An Api I am using allows to set it only when a value it’s really called. Use case:
    function isValidToWrap($value)
    {
    if (….) {
    return “$value is not valid because bar..”;
    }
    if (….) {
    return “$value is not valid because foo..”;
    }
    return true;
    }
    This would allow for more fine-grained validation messages too , although I think it can be done with multiple validators; the problem is the domain model often does not use multiple validators but a single method.

  8. thomasw

    Sunday, December 27, 2009 - 14:02:03

    Simply take a look into ZF’s manual to see how validation messages can be changed. There are multiple ways which all have pro and cons.