Zend_Validate-StringLength.xml 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.validate.set.stringlength">
  4. <title>StringLength</title>
  5. <para>
  6. This validator allows you to validate if a given string is between a defined length.
  7. </para>
  8. <note>
  9. <title>Zend_Validate_StringLength supports only string validation</title>
  10. <para>
  11. It should be noted that Zend_Validate_StringLength supports only the validation of strings.
  12. Integers, floats, dates or objects can not be validated with this validator.
  13. </para>
  14. </note>
  15. <sect3 id="zend.validate.set.stringlength.options">
  16. <title>Supported options for Zend_Validate_StringLength</title>
  17. <para>
  18. The following options are supported for
  19. <classname>Zend_Validate_StringLength</classname>:
  20. </para>
  21. <itemizedlist>
  22. <listitem>
  23. <para>
  24. <emphasis><property>encoding</property></emphasis>: Sets the
  25. <constant>ICONV</constant> encoding which has to be used for this string.
  26. </para>
  27. </listitem>
  28. <listitem>
  29. <para>
  30. <emphasis><property>min</property></emphasis>: Sets the minimum allowed length
  31. for a string.
  32. </para>
  33. </listitem>
  34. <listitem>
  35. <para>
  36. <emphasis><property>max</property></emphasis>: Sets the maximum allowed length
  37. for a string.
  38. </para>
  39. </listitem>
  40. </itemizedlist>
  41. </sect3>
  42. <sect3 id="zend.validate.set.stringlength.basic">
  43. <title>Default behaviour for Zend_Validate_StringLength</title>
  44. <para>
  45. Per default this validator checks if a value is between <property>min</property> and
  46. <property>max</property>. But for <property>min</property> the default value is
  47. <emphasis>0</emphasis> and for <property>max</property> it is
  48. <emphasis><constant>NULL</constant></emphasis> which means unlimited.
  49. </para>
  50. <para>
  51. So per default, without giving any options, this validator only checks if the input
  52. is a string.
  53. </para>
  54. </sect3>
  55. <sect3 id="zend.validate.set.stringlength.maximum">
  56. <title>Limiting the maximum allowed length of a string</title>
  57. <para>
  58. To limit the maximum allowed length of a string you need to set the
  59. <property>max</property> property. It accepts an integer value as input.
  60. </para>
  61. <programlisting language="php"><![CDATA[
  62. $validator = new Zend_Validate_StringLength(array('max' => 6));
  63. $validator->isValid("Test"); // returns true
  64. $validator->isValid("Testing"); // returns false
  65. ]]></programlisting>
  66. <para>
  67. You can set the maximum allowed length also afterwards by using the
  68. <methodname>setMax()</methodname> method. And <methodname>getMax()</methodname> to
  69. retrieve the actual maximum border.
  70. </para>
  71. <programlisting language="php"><![CDATA[
  72. $validator = new Zend_Validate_StringLength();
  73. $validator->setMax(6);
  74. $validator->isValid("Test"); // returns true
  75. $validator->isValid("Testing"); // returns false
  76. ]]></programlisting>
  77. </sect3>
  78. <sect3 id="zend.validate.set.stringlength.minimum">
  79. <title>Limiting the minimal required length of a string</title>
  80. <para>
  81. To limit the minimal required length of a string you need to set the
  82. <property>min</property> property. It accepts also an integer value as input.
  83. </para>
  84. <programlisting language="php"><![CDATA[
  85. $validator = new Zend_Validate_StringLength(array('min' => 5));
  86. $validator->isValid("Test"); // returns false
  87. $validator->isValid("Testing"); // returns true
  88. ]]></programlisting>
  89. <para>
  90. You can set the minimal requested length also afterwards by using the
  91. <methodname>setMin()</methodname> method. And <methodname>getMin()</methodname> to
  92. retrieve the actual minimum border.
  93. </para>
  94. <programlisting language="php"><![CDATA[
  95. $validator = new Zend_Validate_StringLength();
  96. $validator->setMin(5);
  97. $validator->isValid("Test"); // returns false
  98. $validator->isValid("Testing"); // returns true
  99. ]]></programlisting>
  100. </sect3>
  101. <sect3 id="zend.validate.set.stringlength.both">
  102. <title>Limiting a string on both sides</title>
  103. <para>
  104. Sometimes it is required to get a string which has a maximal defined length but which
  105. is also minimal chars long. For example when you have a textbox where a user can enter
  106. his name, then you may want to limit the name to maximum 30 chars but want to get sure
  107. that he entered his name. So you limit the mimimum required length to 3 chars. See the
  108. following example:
  109. </para>
  110. <programlisting language="php"><![CDATA[
  111. $validator = new Zend_Validate_StringLength(array('min' => 3, 'max' => 30));
  112. $validator->isValid("."); // returns false
  113. $validator->isValid("Test"); // returns true
  114. $validator->isValid("Testing"); // returns true
  115. ]]></programlisting>
  116. <note>
  117. <title>Setting a lower maximum border than the minimum border</title>
  118. <para>
  119. When you try to set a lower maximum value as the actual minimum value, or a
  120. higher minimum value as the actual maximum value, then an exception will be
  121. raised.
  122. </para>
  123. </note>
  124. </sect3>
  125. <sect3 id="zend.validate.set.stringlength.encoding">
  126. <title>Encoding of values</title>
  127. <para>
  128. Strings are always using a encoding. Even when you don't set the encoding explicit,
  129. <acronym>PHP</acronym> uses one. When your application is using a different encoding
  130. than <acronym>PHP</acronym> itself then you should set an encoding yourself.
  131. </para>
  132. <para>
  133. You can set your own encoding at initiation with the <property>encoding</property>
  134. option, or by using the <methodname>setEncoding()</methodname> method. We assume that
  135. your installation uses <acronym>ISO</acronym> and your application it set to
  136. <acronym>ISO</acronym>. In this case you will see the below behaviour.
  137. </para>
  138. <programlisting language="php"><![CDATA[
  139. $validator = new Zend_Validate_StringLength(
  140. array('min' => 6)
  141. );
  142. $validator->isValid("Ärger"); // returns false
  143. $validator->setEncoding("UTF-8");
  144. $validator->isValid("Ärger"); // returns true
  145. $validator2 = new Zend_Validate_StringLength(
  146. array('min' => 6, 'encoding' => 'UTF-8')
  147. );
  148. $validator2->isValid("Ärger"); // returns true
  149. ]]></programlisting>
  150. <para>
  151. So when your installation and your application are using different encodings, then you
  152. should always set an encoding yourself.
  153. </para>
  154. </sect3>
  155. </sect2>
  156. <!--
  157. vim:se ts=4 sw=4 et:
  158. -->