Validation of posted values in array

Hello everybody.

I ran into a problem when trying to implement the following:

I have a form that contains three drop downs sharing the same attribute (type). I need to store the selected values of these drop downs in the database as comma separated values. In order to do that I read the values as an array


$_POST['type']['1'], $_POST['type']['2'], $_POST['type']['3']

, implode them and assign them to the attribute


$model->type = implode(',',$_POST['type']);

In order to get the $_POST array fetch me the values I needed, I added the ‘name’ attribute to the $htmlOptions array of each activeDropDownList element in the following way:


<?php 

      echo CHtml::activeDropDownList($model,'type',$list,array('name'=>'Advertisment[type][1]')); 

      echo CHtml::activeDropDownList($model,'type',$list,array('name'=>'Advertisment[type][2]'));

      echo CHtml::activeDropDownList($model,'type',$list,array('name'=>'Advertisment[type][3]')); 

?>

My problem is that the validation rule for the ‘type’ field (array(‘type’,‘required’)) is not working anymore. I presume that this is because I changed the ‘name’ of the drop down element. It should read ‘Advertisment[type]’ but now reads ‘Advertisment[type][1]’…

Any suggestions into resolving this issue would be appreciated.

Thanks in advance,

Jerry

Why don’t you use a checkboxlist or a listbox for this? It can be check the quantity of items in your array and it will works perfectly.

Thanks for your answer sebas.

Unfortunately I cannot have anything else than drop downs since I use two dependent dropdowns, as shown here: http://www.yiiframework.com/doc/cookbook/24/

My exact code is:


<?php

$list = CHtml::listData(PropertyCategory::model()->findAll(array('order'=>'id ASC')),'id','title');


echo EHtml::activeDropDownList($model,'property_category',$list,array(		'class'=>'dropdown',													

		'ajax' => array(

		'type'=>'POST', //request type

		'url'=>'index.php?r=advertisment/getpropertytypes&sel=1', //url to call

		'update'=>'#Advertisment_property_type1',), //selector to update

		'name'=>'Advertisment[property_category][1]',

		'id'=>'Advertisment_property_category1',

		'prompt'=>'-- Select one --'));

 ?>


<?php

$list = CHtml::listData(PropertyType::model()->findAll('category_id=:parent_id',array(':parent_id'=>$model->property_category)),'id','title');


echo EHtml::activeDropDownList($model,'property_type',$list,array(

		'class'=>'dropdown',

		'name'=>'Advertisment[property_type][1]',

		'id'=>'Advertisment_property_type1',));

 ?>