2
0

Zend_Validate-NotEmpty.xml 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.validate.set.notempty">
  4. <title>NotEmpty</title>
  5. <para>
  6. This validator allows you to validate if a given value is not empty. This is often useful
  7. when working with form elements or other user input, where you can use it to ensure required
  8. elements have values associated with them.
  9. </para>
  10. <sect3 id="zend.validate.set.notempty.options">
  11. <title>Supported options for Zend_Validate_NotEmpty</title>
  12. <para>
  13. The following options are supported for <classname>Zend_Validate_NotEmpty</classname>:
  14. </para>
  15. <itemizedlist>
  16. <listitem>
  17. <para>
  18. <emphasis><property>type</property></emphasis>: Sets the type of validation
  19. which will be processed. For details take a look into <link
  20. linkend="zend.validate.set.notempty.types">this section</link>.
  21. </para>
  22. </listitem>
  23. </itemizedlist>
  24. </sect3>
  25. <sect3 id="zend.validate.set.notempty.default">
  26. <title>Default behaviour for Zend_Validate_NotEmpty</title>
  27. <para>
  28. By default, this validator works differently than you would expect when you've worked
  29. with <acronym>PHP</acronym>'s <methodname>empty()</methodname> function. In
  30. particular, this validator will evaluate both the integer <emphasis>0</emphasis> and
  31. string '<emphasis>0</emphasis>' as empty.
  32. </para>
  33. <programlisting language="php"><![CDATA[
  34. $valid = new Zend_Validate_NotEmpty();
  35. $value = '';
  36. $result = $valid->isValid($value);
  37. // returns false
  38. ]]></programlisting>
  39. <note>
  40. <title>Default behaviour differs from PHP</title>
  41. <para>
  42. Without providing configuration, <classname>Zend_Validate_NotEmpty</classname>'s
  43. behaviour differs from <acronym>PHP</acronym>.
  44. </para>
  45. </note>
  46. </sect3>
  47. <sect3 id="zend.validate.set.notempty.types">
  48. <title>Changing behaviour for Zend_Validate_NotEmpty</title>
  49. <para>
  50. Some projects have differing opinions of what is considered an "empty" value: a string
  51. with only whitespace might be considered empty, or <emphasis>0</emphasis> may be
  52. considered non-empty (particularly for boolean sequences). To accomodate differing
  53. needs, <classname>Zend_Validate_NotEmpty</classname> allows you to configure which types
  54. should be validated as empty and which not.
  55. </para>
  56. <para>
  57. The following types can be handled:
  58. </para>
  59. <itemizedlist>
  60. <listitem>
  61. <para>
  62. <emphasis>boolean</emphasis>: Returns <constant>FALSE</constant> when the
  63. boolean value is <constant>FALSE</constant>.
  64. </para>
  65. </listitem>
  66. <listitem>
  67. <para>
  68. <emphasis>integer</emphasis>: Returns <constant>FALSE</constant> when an integer
  69. <emphasis>0</emphasis> value is given. Per default this validation is not
  70. activated and returns <constant>TRUE</constant> on any integer values.
  71. </para>
  72. </listitem>
  73. <listitem>
  74. <para>
  75. <emphasis>float</emphasis>: Returns <constant>FALSE</constant> when an float
  76. <emphasis>0.0</emphasis> value is given. Per default this validation is not
  77. activated and returns <constant>TRUE</constant> on any float values.
  78. </para>
  79. </listitem>
  80. <listitem>
  81. <para>
  82. <emphasis>string</emphasis>: Returns <constant>FALSE</constant> when an empty
  83. string <emphasis>''</emphasis> is given.
  84. </para>
  85. </listitem>
  86. <listitem>
  87. <para>
  88. <emphasis>zero</emphasis>: Returns <constant>FALSE</constant> when the single
  89. character zero (<emphasis>'0'</emphasis>) is given.
  90. </para>
  91. </listitem>
  92. <listitem>
  93. <para>
  94. <emphasis>empty_array</emphasis>: Returns <constant>FALSE</constant> when an
  95. empty <emphasis>array</emphasis> is given.
  96. </para>
  97. </listitem>
  98. <listitem>
  99. <para>
  100. <emphasis>null</emphasis>: Returns <constant>FALSE</constant> when an
  101. <constant>NULL</constant> value is given.
  102. </para>
  103. </listitem>
  104. <listitem>
  105. <para>
  106. <emphasis>php</emphasis>: Returns <constant>FALSE</constant> on the same reasons
  107. where <acronym>PHP</acronym> method <methodname>empty()</methodname> would
  108. return <constant>TRUE</constant>.
  109. </para>
  110. </listitem>
  111. <listitem>
  112. <para>
  113. <emphasis>space</emphasis>: Returns <constant>FALSE</constant> when an string
  114. is given which contains only whitespaces.
  115. </para>
  116. </listitem>
  117. <listitem>
  118. <para>
  119. <emphasis>object</emphasis>: Returns <constant>TRUE</constant>.
  120. <constant>FALSE</constant> will be returned when <property>object</property> is
  121. not allowed but an object is given.
  122. </para>
  123. </listitem>
  124. <listitem>
  125. <para>
  126. <emphasis>object_string</emphasis>: Returns <constant>FALSE</constant> when an
  127. object is given and it's <methodname>__toString()</methodname> method returns an
  128. empty string.
  129. </para>
  130. </listitem>
  131. <listitem>
  132. <para>
  133. <emphasis>object_count</emphasis>: Returns <constant>FALSE</constant> when an
  134. object is given, it has an <classname>Countable</classname> interface and it's
  135. count is 0.
  136. </para>
  137. </listitem>
  138. <listitem>
  139. <para>
  140. <emphasis>all</emphasis>: Returns <constant>FALSE</constant> on all above types.
  141. </para>
  142. </listitem>
  143. </itemizedlist>
  144. <para>
  145. All other given values will return <constant>TRUE</constant> per default.
  146. </para>
  147. <para>
  148. There are several ways to select which of the above types are validated. You can give
  149. one or multiple types and add them, you can give an array, you can use constants, or you
  150. can give a textual string. See the following examples:
  151. </para>
  152. <programlisting language="php"><![CDATA[
  153. // Returns false on 0
  154. $validator = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::INTEGER);
  155. // Returns false on 0 or '0'
  156. $validator = new Zend_Validate_NotEmpty(
  157. Zend_Validate_NotEmpty::INTEGER + Zend_NotEmpty::ZERO
  158. );
  159. // Returns false on 0 or '0'
  160. $validator = new Zend_Validate_NotEmpty(array(
  161. Zend_Validate_NotEmpty::INTEGER,
  162. Zend_Validate_NotEmpty::ZERO
  163. ));
  164. // Returns false on 0 or '0'
  165. $validator = new Zend_Validate_NotEmpty(array(
  166. 'integer',
  167. 'zero',
  168. ));
  169. ]]></programlisting>
  170. <para>
  171. You can also provide an instance of <classname>Zend_Config</classname> to set the
  172. desired types. To set types after instantiation, use the
  173. <methodname>setType()</methodname> method.
  174. </para>
  175. </sect3>
  176. </sect2>
  177. <!--
  178. vim:se ts=4 sw=4 et:
  179. -->