Zend_Validate-StringLength.xml 7.0 KB

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