Zend_Validate-Callback.xml 7.1 KB

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