Zend_Validate-Callback.xml 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.validate.set.callback">
  4. <title>Callback</title>
  5. <para>
  6. <classname>Zend_Validate_Callback</classname> allows you to provide a callback with which to
  7. validate a given value.
  8. </para>
  9. <sect3 id="zend.validate.set.callback.basic">
  10. <title>Basic usage</title>
  11. <para>
  12. The simplest usecase is to have a single function and use it as a callback. Let's expect
  13. we have the following function.
  14. </para>
  15. <programlisting language="php"><![CDATA[
  16. function myMethod($value)
  17. {
  18. // some validation
  19. return true;
  20. }
  21. ]]></programlisting>
  22. <para>
  23. To use it within <classname>Zend_Validate_Callback</classname> you just have to call it
  24. this way:
  25. </para>
  26. <programlisting language="php"><![CDATA[
  27. $valid = new Zend_Validate_Callback('myMethod');
  28. if ($valid->isValid($input)) {
  29. // input appears to be valid
  30. } else {
  31. // input is invalid
  32. }
  33. ]]></programlisting>
  34. </sect3>
  35. <sect3 id="zend.validate.set.callback.closure">
  36. <title>Usage with closures</title>
  37. <para>
  38. PHP 5.3 introduces <ulink url="http://php.net/functions.anonymous">closures</ulink>,
  39. which are basically self-contained or <emphasis>anonymous</emphasis> functions. PHP
  40. considers closures another form of callback, and, as such, may be used with
  41. <classname>Zend_Validate_Callback</classname>. As an example:
  42. </para>
  43. <programlisting language="php"><![CDATA[
  44. $valid = new Zend_Validate_Callback(function($value){
  45. // some validation
  46. return true;
  47. });
  48. if ($valid->isValid($input)) {
  49. // input appears to be valid
  50. } else {
  51. // input is invalid
  52. }
  53. ]]></programlisting>
  54. </sect3>
  55. <sect3 id="zend.validate.set.callback.class">
  56. <title>Usage with class-based callbacks</title>
  57. <para>
  58. Of course it's also possible to use a class method as callback. Let's expect we have
  59. the following class method:
  60. </para>
  61. <programlisting language="php"><![CDATA[
  62. class MyClass
  63. {
  64. public function myMethod($value)
  65. {
  66. // some validation
  67. return true;
  68. }
  69. }
  70. ]]></programlisting>
  71. <para>
  72. The definition of the callback is in this case almost the same. You have just to create
  73. an instance of the class before the method and create an array describing the callback:
  74. </para>
  75. <programlisting language="php"><![CDATA[
  76. $object = new MyClass;
  77. $valid = new Zend_Validate_Callback(array($object, 'myMethod'));
  78. if ($valid->isValid($input)) {
  79. // input appears to be valid
  80. } else {
  81. // input is invalid
  82. }
  83. ]]></programlisting>
  84. <para>
  85. You may also define a static method as a callback. Consider the following class
  86. definition and validator usage:
  87. </para>
  88. <programlisting language="php"><![CDATA[
  89. class MyClass
  90. {
  91. public static function test($value)
  92. {
  93. // some validation
  94. return true;
  95. }
  96. }
  97. $valid = new Zend_Validate_Callback(array('MyClass, 'test'));
  98. if ($valid->isValid($input)) {
  99. // input appears to be valid
  100. } else {
  101. // input is invalid
  102. }
  103. ]]></programlisting>
  104. <para>
  105. Finally, if you are using PHP 5.3, you may define the magic method
  106. <methodname>__invoke()</methodname> in your class. If you do so, simply providing an
  107. instance of the class as the callback will also work:
  108. </para>
  109. <programlisting language="php"><![CDATA[
  110. class MyClass
  111. {
  112. public function __invoke($value)
  113. {
  114. // some validation
  115. return true;
  116. }
  117. }
  118. $object = new MyClass();
  119. $valid = new Zend_Validate_Callback($object);
  120. if ($valid->isValid($input)) {
  121. // input appears to be valid
  122. } else {
  123. // input is invalid
  124. }
  125. ]]></programlisting>
  126. </sect3>
  127. <sect3 id="zend.validate.set.callback.options">
  128. <title>Adding options</title>
  129. <para>
  130. <classname>Zend_Validate_Callback</classname> also allows the usage of options which
  131. are provided as additional arguments to the callback.
  132. </para>
  133. <para>
  134. Consider the following class and method definition:
  135. </para>
  136. <programlisting language="php"><![CDATA[
  137. class MyClass
  138. {
  139. function myMethod($value, $option)
  140. {
  141. // some validation
  142. return true;
  143. }
  144. }
  145. ]]></programlisting>
  146. <para>
  147. There are two ways to inform the validator of additional options: pass them in the
  148. constructor, or pass them to the <methodname>setOptions()</methodname> method.
  149. </para>
  150. <para>
  151. To pass them to the constructor, you would need to pass an array containing two keys,
  152. "callback" and "options":
  153. </para>
  154. <programlisting language="php"><![CDATA[
  155. $valid = new Zend_Validate_Callback(array(
  156. 'callback' => array('MyClass', 'myMethod'),
  157. 'options' => $option,
  158. ));
  159. if ($valid->isValid($input)) {
  160. // input appears to be valid
  161. } else {
  162. // input is invalid
  163. }
  164. ]]></programlisting>
  165. <para>
  166. Otherwise, you may pass them to the validator after instantiation:
  167. </para>
  168. <programlisting language="php"><![CDATA[
  169. $valid = new Zend_Validate_Callback(array('MyClass', 'myMethod'));
  170. $valid->setOptions($option);
  171. if ($valid->isValid($input)) {
  172. // input appears to be valid
  173. } else {
  174. // input is invalid
  175. }
  176. ]]></programlisting>
  177. <para>
  178. When there are additional values given to <methodname>isValid()</methodname> then these
  179. values will be added immediately after <varname>$value</varname>.
  180. </para>
  181. <programlisting language="php"><![CDATA[
  182. $valid = new Zend_Validate_Callback(array('MyClass', 'myMethod'));
  183. $valid->setOptions($option);
  184. if ($valid->isValid($input, $additional)) {
  185. // input appears to be valid
  186. } else {
  187. // input is invalid
  188. }
  189. ]]></programlisting>
  190. <para>
  191. When making the call to the callback, the value to be validated will always be passed as
  192. the first argument to the callback followed by all other values given to
  193. <methodname>isValid()</methodname>; all other options will follow it. The amount and
  194. type of options which can be used is not limited.
  195. </para>
  196. </sect3>
  197. </sect2>
  198. <!--
  199. vim:se ts=4 sw=4 et:
  200. -->