Zend_Validate-NotEmpty.xml 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.default">
  11. <title>Default behaviour for Zend_Validate_NotEmpty</title>
  12. <para>
  13. By default, this validator works differently than you would expect when you've worked
  14. with <acronym>PHP</acronym>'s <methodname>empty()</methodname> function. In
  15. particular, this validator will evaluate both the integer <emphasis>0</emphasis> and
  16. string '<emphasis>0</emphasis>' as empty.
  17. </para>
  18. <programlisting language="php"><![CDATA[
  19. $valid = new Zend_Validate_NotEmpty();
  20. $value = '';
  21. $result = $valid->isValid($value);
  22. // returns false
  23. ]]></programlisting>
  24. <note>
  25. <title>Default behaviour differs from PHP</title>
  26. <para>
  27. Without providing configuration, <classname>Zend_Validate_NotEmpty</classname>'s
  28. behaviour differs from <acronym>PHP</acronym>.
  29. </para>
  30. </note>
  31. </sect3>
  32. <sect3 id="zend.validate.set.notempty.types">
  33. <title>Changing behaviour for Zend_Validate_NotEmpty</title>
  34. <para>
  35. Some projects have differing opinions of what is considered an "empty" value: a string
  36. with only whitespace might be considered empty, or <emphasis>0</emphasis> may be
  37. considered non-empty (particularly for boolean sequences). To accomodate differing
  38. needs, <classname>Zend_Validate_NotEmpty</classname> allows you to configure which types
  39. should be validated as empty and which not.
  40. </para>
  41. <para>
  42. The following types can be handled:
  43. </para>
  44. <itemizedlist>
  45. <listitem>
  46. <para>
  47. <emphasis>boolean</emphasis>: Returns <constant>FALSE</constant> when the
  48. boolean value is <constant>FALSE</constant>.
  49. </para>
  50. </listitem>
  51. <listitem>
  52. <para>
  53. <emphasis>integer</emphasis>: Returns <constant>FALSE</constant> when an integer
  54. <emphasis>0</emphasis> value is given. Per default this validation is not
  55. activated and returns <constant>TRUE</constant> on any integer values.
  56. </para>
  57. </listitem>
  58. <listitem>
  59. <para>
  60. <emphasis>float</emphasis>: Returns <constant>FALSE</constant> when an float
  61. <emphasis>0.0</emphasis> value is given. Per default this validation is not
  62. activated and returns <constant>TRUE</constant> on any float values.
  63. </para>
  64. </listitem>
  65. <listitem>
  66. <para>
  67. <emphasis>string</emphasis>: Returns <constant>FALSE</constant> when an empty
  68. string <emphasis>''</emphasis> is given.
  69. </para>
  70. </listitem>
  71. <listitem>
  72. <para>
  73. <emphasis>zero</emphasis>: Returns <constant>FALSE</constant> when the single
  74. character zero (<emphasis>'0'</emphasis>) is given.
  75. </para>
  76. </listitem>
  77. <listitem>
  78. <para>
  79. <emphasis>empty_array</emphasis>: Returns <constant>FALSE</constant> when an
  80. empty <emphasis>array</emphasis> is given.
  81. </para>
  82. </listitem>
  83. <listitem>
  84. <para>
  85. <emphasis>null</emphasis>: Returns <constant>FALSE</constant> when an
  86. <constant>NULL</constant> value is given.
  87. </para>
  88. </listitem>
  89. <listitem>
  90. <para>
  91. <emphasis>php</emphasis>: Returns <constant>FALSE</constant> on the same reasons
  92. where <acronym>PHP</acronym> method <methodname>empty()</methodname> would
  93. return <constant>TRUE</constant>.
  94. </para>
  95. </listitem>
  96. <listitem>
  97. <para>
  98. <emphasis>space</emphasis>: Returns <constant>FALSE</constant> when an string
  99. is given which contains only whitespaces.
  100. </para>
  101. </listitem>
  102. <listitem>
  103. <para>
  104. <emphasis>all</emphasis>: Returns <constant>FALSE</constant> on all above types.
  105. </para>
  106. </listitem>
  107. </itemizedlist>
  108. <para>
  109. All other given values will return <constant>TRUE</constant> per default.
  110. </para>
  111. <para>
  112. There are several ways to select which of the above types are validated. You can give
  113. one or multiple types and add them, you can give an array, you can use constants, or you
  114. can give a textual string. See the following examples:
  115. </para>
  116. <programlisting language="php"><![CDATA[
  117. // Returns false on 0
  118. $validator = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::INTEGER);
  119. // Returns false on 0 or '0'
  120. $validator = new Zend_Validate_NotEmpty(
  121. Zend_Validate_NotEmpty::INTEGER + Zend_NotEmpty::ZERO
  122. );
  123. // Returns false on 0 or '0'
  124. $validator = new Zend_Validate_NotEmpty(array(
  125. Zend_Validate_NotEmpty::INTEGER,
  126. Zend_Validate_NotEmpty::ZERO
  127. ));
  128. // Returns false on 0 or '0'
  129. $validator = new Zend_Validate_NotEmpty(array(
  130. 'integer',
  131. 'zero',
  132. ));
  133. ]]></programlisting>
  134. <para>
  135. You can also provide an instance of <classname>Zend_Config</classname> to set the
  136. desired types. To set types after instantiation, use the
  137. <methodname>setType()</methodname> method.
  138. </para>
  139. </sect3>
  140. </sect2>
  141. <!--
  142. vim:se ts=4 sw=4 et:
  143. -->