Zend_Validate-InArray.xml 4.5 KB

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