array validation for model fields

Hello All,

I am working on Multiple Addresses functionality in which there should be multiple zips, multiple cities. I have created one address array and i am storing all the values into this array. But the problem is for validating zip codes, cities i am facing problem because zip field, city field is array so yii validation is not working . Is there way to solve this problem? how can i validate those array fields using yii?

My sample code for city :

<div class="row">

&lt;?php echo &#036;form-&gt;labelEx(&#036;model, 'city'); ?&gt;


&lt;?php echo CHtml::textField(get_class(&#036;model).'[city][]', &#036;model-&gt;city); ?&gt;


&lt;?php echo &#036;form-&gt;error(&#036;model, 'city'); ?&gt;

</div>

Thanks in advance,

Ganesh

Can you show me your complete code of view and controller file so that i can understand exactly what you are trying to do.!!!

Since if you are talking about Dynamic form then it is a different case.So,share your code here.so, people can answer your very well.

This is my view file code.

<div class="MainAddress">

<?php

  /**


   * Loop for multiple addresses


   */


  foreach(&#036;this-&gt;fetchAddresses() as &#036;key =&gt; &#036;address) {


    &#036;model-&gt;address = &#036;address['address'];


    &#036;model-&gt;city = &#036;address['city'];


    &#036;model-&gt;zip = &#036;address['zip'];

?>

&lt;div class=&quot;divAddress&quot;&gt;


    &lt;div class=&quot;row&quot;&gt;


        &lt;?php echo &#036;form-&gt;labelEx(&#036;model, 'address'); ?&gt;


        &lt;?php echo CHtml::textArea(get_class(&#036;model).'[address][]', &#036;model-&gt;address, array('class'=&gt;'addressField'));?&gt;


        &lt;?php echo &#036;form-&gt;error(&#036;model, 'address'); ?&gt;


    &lt;/div&gt;


    &lt;div class=&quot;row&quot;&gt;


		&lt;?php echo &#036;form-&gt;labelEx(&#036;model, 'city'); ?&gt;


		&lt;?php echo CHtml::textField(get_class(&#036;model).'[city][]', &#036;model-&gt;city); ?&gt;


		&lt;?php echo &#036;form-&gt;error(&#036;model, 'city'); ?&gt;


    &lt;/div&gt;


    &lt;div class=&quot;row&quot;&gt;


		&lt;?php echo &#036;form-&gt;labelEx(&#036;model, 'zip'); ?&gt;


		&lt;?php echo CHtml::textField(get_class(&#036;model).'[zip][]', &#036;model-&gt;zip); ?&gt;


		&lt;?php echo &#036;form-&gt;error(&#036;model, 'zip'); ?&gt;


	&lt;/div&gt;


&lt;/div&gt; 

<?php

} 

?>

</div>

<div class="column">

&lt;div class=&quot;address-button right&quot; &gt;


    &lt;input type=&quot;button&quot; class=&quot;addmore&quot; id=&quot;addmore&quot; value=&quot;Add Multiple Address&quot;/&gt;


&lt;/div&gt;

</div>

I have write down the fetchAddresss() method from which i have fetched all the address.


	// Code to fetch Addresses





   &#036;model = &#036;this-&gt;getFormModel();





    &#036;arrAddresses = array(


        array(


            'address' =&gt; '',


            'city' =&gt; '',


            'zip' =&gt; ''


        )


    );





    /**


     * fetch addresses 


     */


    &#036;arrAddress = CJSON::decode(&#036;model-&gt;address);


    if (&#33;empty(&#036;arrAddress) &amp;&amp; is_array(&#036;arrAddress)) {


        /**


         * Get city and zipcode information


         */


        &#036;arrCity = (is_array(&#036;model-&gt;city))?&#036;model-&gt;city:&#092;CJSON::decode(&#036;model-&gt;city);


        &#036;arrZipcodes = (is_array(&#036;model-&gt;zip))?&#036;model-&gt;zip:&#092;CJSON::decode(&#036;model-&gt;zip);


        /**


         * Add city  and zipcode


         */


        array_walk(&#036;arrAddress, function(&#036;value, &#036;key)use(&amp;&#036;arrAddress, &amp;&#036;arrCity, &amp;&#036;arrCountry, &amp;&#036;arrState, &amp;&#036;arrZipcodes) {


                    &#036;arrAddress[&#036;key] = array(


                        'address' =&gt; &#036;arrAddress[&#036;key],


                        'city' =&gt; &#036;arrCity[&#036;key],


                        'country' =&gt; &#036;arrCountry[&#036;key],


                        'state' =&gt; &#036;arrState[&#036;key],


                        'zip' =&gt; &#036;arrZipcodes[&#036;key]


                    );


                });


        &#036;arrAddresses = &#036;arrAddress;


    }





    /**


     * Return addresses


     */


    return &#036;arrAddresses;

Then using magic method(get,set) i have saved the addresses. Now i want to use required rules for city and zip. but its not working.