Zend_Validate-InArray.xml 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 16394 -->
  3. <!-- Reviewed: no -->
  4. <sect2 id="zend.validate.set.in_array">
  5. <title>InArray</title>
  6. <para>
  7. <classname>Zend_Validate_InArray</classname> allows you to validate if a given value is
  8. contained within an array. It is also able to validate multidimensional arrays.
  9. </para>
  10. <sect3 id="zend.validate.set.in_array.basic">
  11. <title>Simple array validation</title>
  12. <para>
  13. The simplest way, is just to give the array which should be searched against at
  14. initiation:
  15. </para>
  16. <programlisting language="php"><![CDATA[
  17. $validator = new Zend_Validate_InArray(array('key' => 'value', 'otherkey' => 'othervalue'));
  18. if ($validator->isValid('value')) {
  19. // value found
  20. } else {
  21. // no value found
  22. }
  23. ]]></programlisting>
  24. <para>
  25. This will behave exactly like <acronym>PHP</acronym>'s <methodname>in_array()</methodname> method.
  26. </para>
  27. <note>
  28. <para>
  29. Per default this validation is not strict nor can it validate multidimensional arrays.
  30. </para>
  31. </note>
  32. <para>
  33. Of course you can give the array to validate against also afterwards by using the
  34. <methodname>setHaystack()</methodname> method.
  35. </para>
  36. <programlisting language="php"><![CDATA[
  37. $validator = new Zend_Validate_InArray();
  38. $validator->setHaystack(array('key' => 'value', 'otherkey' => 'othervalue'));
  39. if ($validator->isValid('value')) {
  40. // value found
  41. } else {
  42. // no value found
  43. }
  44. ]]></programlisting>
  45. </sect3>
  46. <sect3 id="zend.validate.set.in_array.strict">
  47. <title>Strict array validation</title>
  48. <para>
  49. As mentioned before you can also do a strict validation within the array. Per default there
  50. would be no difference between the integer value <emphasis>0</emphasis> and the string
  51. <emphasis>"0"</emphasis>. When doing a strict validation this difference will also be
  52. validated and only same types are accepted.
  53. </para>
  54. <para>
  55. A strict validation can also be done by using two different ways. At initiation and by using
  56. a method. At initiation you have to give an array with the following structure:
  57. </para>
  58. <programlisting language="php"><![CDATA[
  59. $validator = new Zend_Validate_InArray(
  60. array(
  61. 'haystack' => array('key' => 'value', 'otherkey' => 'othervalue'),
  62. 'strict' => true
  63. )
  64. );
  65. if ($validator->isValid('value')) {
  66. // value found
  67. } else {
  68. // no value found
  69. }
  70. ]]></programlisting>
  71. <para>
  72. The <emphasis>haystack</emphasis> key contains your array to validate against, and by
  73. setting the <emphasis>script</emphasis> key to <constant>TRUE</constant> the validation
  74. is done by using a strict type check.
  75. </para>
  76. <para>
  77. Of course you can also use the <methodname>setStrict()</methodname> method to change
  78. this setting afterwards.
  79. </para>
  80. <note>
  81. <para>
  82. Note that the <emphasis>strict</emphasis> setting is per default
  83. <constant>FALSE</constant>.
  84. </para>
  85. </note>
  86. </sect3>
  87. <sect3 id="zend.validate.set.in_array.recursive">
  88. <title>Recursive array validation</title>
  89. <para>
  90. In addition to <acronym>PHP</acronym>'s <methodname>in_array()</methodname> method
  91. this validator can also be used to validate multidimensional arrays.
  92. </para>
  93. <para>
  94. To validate multidimensional arrays you have to set the <emphasis>recursive</emphasis>
  95. option.
  96. </para>
  97. <programlisting language="php"><![CDATA[
  98. $validator = new Zend_Validate_InArray(
  99. array(
  100. 'haystack' => array(
  101. 'firstDimension' => array('key' => 'value', 'otherkey' => 'othervalue'),
  102. 'secondDimension' => array('some' => 'real', 'different' => 'key')),
  103. 'recursive' => true
  104. )
  105. );
  106. if ($validator->isValid('value')) {
  107. // value found
  108. } else {
  109. // no value found
  110. }
  111. ]]></programlisting>
  112. <para>
  113. Your array will then be validated recursive to see if the given value is contained.
  114. </para>
  115. <note>
  116. <para>
  117. Note that per default the recursive validation is turned off.
  118. </para>
  119. </note>
  120. </sect3>
  121. </sect2>
  122. <!--
  123. vim:se ts=4 sw=4 et:
  124. -->