1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect2 id="zend.validate.set.in_array">
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.
11 <sect3 id="zend.validate.set.in_array.options">
12 <title>Supported options for Zend_Validate_InArray</title>
15 The following options are supported for <classname>Zend_Validate_InArray</classname>:
21 <emphasis><property>haystack</property></emphasis>: Sets the haystack for the
28 <emphasis><property>recursive</property></emphasis>: Defines if the validation
29 should be done recursive. This option defaults to <constant>FALSE</constant>.
35 <emphasis><property>strict</property></emphasis>: Defines if the validation
36 should be done strict. This option defaults to <constant>FALSE</constant>.
42 <sect3 id="zend.validate.set.in_array.basic">
43 <title>Simple array validation</title>
46 The simplest way, is just to give the array which should be searched against at
50 <programlisting language="php"><![CDATA[
51 $validator = new Zend_Validate_InArray(array('key' => 'value',
52 'otherkey' => 'othervalue'));
53 if ($validator->isValid('value')) {
61 This will behave exactly like <acronym>PHP</acronym>'s
62 <methodname>in_array()</methodname> method.
67 Per default this validation is not strict nor can it validate multidimensional
73 Of course you can give the array to validate against also afterwards by using the
74 <methodname>setHaystack()</methodname> method. <methodname>getHaystack()</methodname>
75 returns the actual set haystack array.
78 <programlisting language="php"><![CDATA[
79 $validator = new Zend_Validate_InArray();
80 $validator->setHaystack(array('key' => 'value', 'otherkey' => 'othervalue'));
82 if ($validator->isValid('value')) {
90 <sect3 id="zend.validate.set.in_array.strict">
91 <title>Strict array validation</title>
94 As mentioned before you can also do a strict validation within the array. Per default
95 there would be no difference between the integer value <emphasis>0</emphasis> and the
96 string <emphasis>"0"</emphasis>. When doing a strict validation this difference will
97 also be validated and only same types are accepted.
101 A strict validation can also be done by using two different ways. At initiation and by
102 using a method. At initiation you have to give an array with the following structure:
105 <programlisting language="php"><![CDATA[
106 $validator = new Zend_Validate_InArray(
108 'haystack' => array('key' => 'value', 'otherkey' => 'othervalue'),
113 if ($validator->isValid('value')) {
121 The <emphasis>haystack</emphasis> key contains your array to validate against. And by
122 setting the <emphasis>strict</emphasis> key to <constant>TRUE</constant>, the validation
123 is done by using a strict type check.
127 Of course you can also use the <methodname>setStrict()</methodname> method to change
128 this setting afterwards and <methodname>getStrict()</methodname> to get the actual set
134 Note that the <emphasis>strict</emphasis> setting is per default
135 <constant>FALSE</constant>.
140 <sect3 id="zend.validate.set.in_array.recursive">
141 <title>Recursive array validation</title>
144 In addition to <acronym>PHP</acronym>'s <methodname>in_array()</methodname> method
145 this validator can also be used to validate multidimensional arrays.
149 To validate multidimensional arrays you have to set the <emphasis>recursive</emphasis>
153 <programlisting language="php"><![CDATA[
154 $validator = new Zend_Validate_InArray(
157 'firstDimension' => array('key' => 'value',
158 'otherkey' => 'othervalue'),
159 'secondDimension' => array('some' => 'real',
160 'different' => 'key')),
165 if ($validator->isValid('value')) {
173 Your array will then be validated recursive to see if the given value is contained.
174 Additionally you could use <methodname>setRecursive()</methodname> to set this option
175 afterwards and <methodname>getRecursive()</methodname> to retrieve it.
178 <programlisting language="php"><![CDATA[
179 $validator = new Zend_Validate_InArray(
181 'firstDimension' => array('key' => 'value',
182 'otherkey' => 'othervalue'),
183 'secondDimension' => array('some' => 'real',
184 'different' => 'key')
187 $validator->setRecursive(true);
189 if ($validator->isValid('value')) {
197 <title>Default setting for recursion</title>
200 Per default the recursive validation is turned off.
205 <title>Option keys within the haystack</title>
208 When you are using the keys '<property>haystack</property>',
209 '<property>strict</property>' or '<property>recursive</property>' within your
210 haystack, then you must wrap the <property>haystack</property> key.