Zend_Validate-Callback.xml 7.2 KB

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