Zend_Validate-InArray.xml 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.validate.set.in_array">
  4. <title>InArray</title>
  5. <para>
  6. <classname>Zend_Validate_InArray</classname> allows you to validate if a given value is
  7. contained within an array. It is also able to validate multidimensional arrays.
  8. </para>
  9. <sect3 id="zend.validate.set.in_array.basic">
  10. <title>Simple array validation</title>
  11. <para>
  12. The simplest way, is just to give the array which should be searched against at
  13. initiation:
  14. </para>
  15. <programlisting language="php"><![CDATA[
  16. $validator = new Zend_Validate_InArray(array('key' => 'value',
  17. '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>strict</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',
  102. 'otherkey' => 'othervalue'),
  103. 'secondDimension' => array('some' => 'real',
  104. 'different' => 'key')),
  105. 'recursive' => true
  106. )
  107. );
  108. if ($validator->isValid('value')) {
  109. // value found
  110. } else {
  111. // no value found
  112. }
  113. ]]></programlisting>
  114. <para>
  115. Your array will then be validated recursive to see if the given value is contained.
  116. </para>
  117. <note>
  118. <para>
  119. Note that per default the recursive validation is turned off.
  120. </para>
  121. </note>
  122. </sect3>
  123. </sect2>
  124. <!--
  125. vim:se ts=4 sw=4 et:
  126. -->