1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect2 id="zend.validate.set.callback">
4 <title>Callback</title>
7 <classname>Zend_Validate_Callback</classname> allows you to provide a callback with which to
8 validate a given value.
11 <sect3 id="zend.validate.set.callback.options">
12 <title>Supported options for Zend_Validate_Callback</title>
15 The following options are supported for <classname>Zend_Validate_Callback</classname>:
21 <emphasis><property>callback</property></emphasis>: Sets the callback which will
22 be called for the validation.
28 <emphasis><property>options</property></emphasis>: Sets the additional options
29 which will be given to the callback.
35 <sect3 id="zend.validate.set.callback.basic">
36 <title>Basic usage</title>
39 The simplest usecase is to have a single function and use it as a callback. Let's expect
40 we have the following function.
43 <programlisting language="php"><![CDATA[
44 function myMethod($value)
52 To use it within <classname>Zend_Validate_Callback</classname> you just have to call it
56 <programlisting language="php"><![CDATA[
57 $valid = new Zend_Validate_Callback('myMethod');
58 if ($valid->isValid($input)) {
59 // input appears to be valid
66 <sect3 id="zend.validate.set.callback.closure">
67 <title>Usage with closures</title>
70 <acronym>PHP</acronym> 5.3 introduces <ulink
71 url="http://php.net/functions.anonymous">closures</ulink>, which are basically
72 self-contained or <emphasis>anonymous</emphasis> functions. <acronym>PHP</acronym>
73 considers closures another form of callback, and, as such, may be used with
74 <classname>Zend_Validate_Callback</classname>. As an example:
77 <programlisting language="php"><![CDATA[
78 $valid = new Zend_Validate_Callback(function($value){
83 if ($valid->isValid($input)) {
84 // input appears to be valid
91 <sect3 id="zend.validate.set.callback.class">
92 <title>Usage with class-based callbacks</title>
95 Of course it's also possible to use a class method as callback. Let's expect we have
96 the following class method:
99 <programlisting language="php"><![CDATA[
102 public function myMethod($value)
111 The definition of the callback is in this case almost the same. You have just to create
112 an instance of the class before the method and create an array describing the callback:
115 <programlisting language="php"><![CDATA[
116 $object = new MyClass;
117 $valid = new Zend_Validate_Callback(array($object, 'myMethod'));
118 if ($valid->isValid($input)) {
119 // input appears to be valid
126 You may also define a static method as a callback. Consider the following class
127 definition and validator usage:
130 <programlisting language="php"><![CDATA[
133 public static function test($value)
140 $valid = new Zend_Validate_Callback(array('MyClass', 'test'));
141 if ($valid->isValid($input)) {
142 // input appears to be valid
149 Finally, if you are using <acronym>PHP</acronym> 5.3, you may define the magic method
150 <methodname>__invoke()</methodname> in your class. If you do so, simply providing an
151 instance of the class as the callback will also work:
154 <programlisting language="php"><![CDATA[
157 public function __invoke($value)
164 $object = new MyClass();
165 $valid = new Zend_Validate_Callback($object);
166 if ($valid->isValid($input)) {
167 // input appears to be valid
174 <sect3 id="zend.validate.set.callback.options2">
175 <title>Adding options</title>
178 <classname>Zend_Validate_Callback</classname> also allows the usage of options which
179 are provided as additional arguments to the callback.
183 Consider the following class and method definition:
186 <programlisting language="php"><![CDATA[
189 function myMethod($value, $option)
198 There are two ways to inform the validator of additional options: pass them in the
199 constructor, or pass them to the <methodname>setOptions()</methodname> method.
203 To pass them to the constructor, you would need to pass an array containing two keys,
204 "callback" and "options":
207 <programlisting language="php"><![CDATA[
208 $valid = new Zend_Validate_Callback(array(
209 'callback' => array('MyClass', 'myMethod'),
210 'options' => $option,
213 if ($valid->isValid($input)) {
214 // input appears to be valid
221 Otherwise, you may pass them to the validator after instantiation:
224 <programlisting language="php"><![CDATA[
225 $valid = new Zend_Validate_Callback(array('MyClass', 'myMethod'));
226 $valid->setOptions($option);
228 if ($valid->isValid($input)) {
229 // input appears to be valid
236 When there are additional values given to <methodname>isValid()</methodname> then these
237 values will be added immediately after <varname>$value</varname>.
240 <programlisting language="php"><![CDATA[
241 $valid = new Zend_Validate_Callback(array('MyClass', 'myMethod'));
242 $valid->setOptions($option);
244 if ($valid->isValid($input, $additional)) {
245 // input appears to be valid
252 When making the call to the callback, the value to be validated will always be passed as
253 the first argument to the callback followed by all other values given to
254 <methodname>isValid()</methodname>; all other options will follow it. The amount and
255 type of options which can be used is not limited.