Callback
Zend_Validate_Callback allows you to provide a callback with which to
validate a given value.
Supported options for Zend_Validate_Callback
The following options are supported for Zend_Validate_Callback:
callback: Sets the callback which will
be called for the validation.
options: Sets the additional options
which will be given to the callback.
Basic usage
The simplest usecase is to have a single function and use it as a callback. Let's expect
we have the following function.
To use it within Zend_Validate_Callback you just have to call it
this way:
isValid($input)) {
// input appears to be valid
} else {
// input is invalid
}
]]>
Usage with closures
PHP 5.3 introduces closures, which are basically
self-contained or anonymous functions. PHP
considers closures another form of callback, and, as such, may be used with
Zend_Validate_Callback. As an example:
isValid($input)) {
// input appears to be valid
} else {
// input is invalid
}
]]>
Usage with class-based callbacks
Of course it's also possible to use a class method as callback. Let's expect we have
the following class method:
The definition of the callback is in this case almost the same. You have just to create
an instance of the class before the method and create an array describing the callback:
isValid($input)) {
// input appears to be valid
} else {
// input is invalid
}
]]>
You may also define a static method as a callback. Consider the following class
definition and validator usage:
isValid($input)) {
// input appears to be valid
} else {
// input is invalid
}
]]>
Finally, if you are using PHP 5.3, you may define the magic method
__invoke() in your class. If you do so, simply providing an
instance of the class as the callback will also work:
isValid($input)) {
// input appears to be valid
} else {
// input is invalid
}
]]>
Adding options
Zend_Validate_Callback also allows the usage of options which
are provided as additional arguments to the callback.
Consider the following class and method definition:
There are two ways to inform the validator of additional options: pass them in the
constructor, or pass them to the setOptions() method.
To pass them to the constructor, you would need to pass an array containing two keys,
"callback" and "options":
array('MyClass', 'myMethod'),
'options' => $option,
));
if ($valid->isValid($input)) {
// input appears to be valid
} else {
// input is invalid
}
]]>
Otherwise, you may pass them to the validator after instantiation:
setOptions($option);
if ($valid->isValid($input)) {
// input appears to be valid
} else {
// input is invalid
}
]]>
When there are additional values given to isValid() then these
values will be added immediately after $value.
setOptions($option);
if ($valid->isValid($input, $additional)) {
// input appears to be valid
} else {
// input is invalid
}
]]>
When making the call to the callback, the value to be validated will always be passed as
the first argument to the callback followed by all other values given to
isValid(); all other options will follow it. The amount and
type of options which can be used is not limited.