I have been hitting my head against this problem all night.
What I need is a simple multiple selection list box that is dependent on a drop down list. But there are some other fields on the form that need to be validated. I have the whole thing working just fine until it is time to validate (or re-render for any other reason).
The controlling drop down list and all the other fields display very nicely along with explanatory messages when I use yii’s standard validation. However, the ajax routine that populates the dependent list box is not called unless a user manually changes the controlling drop down list, and thus both the controlling drop down field and dependent multiple list box fields must be reselected by the user every time there is any validation error in any of the other fields.
Is there any way to invoke the ajax routine that propagates the dependent list box whenever there are any validation errors without user intervention?
Here is my view code:
$form=$this->beginWidget('CActiveForm', array(
'id'=>'working-block-model-element-display-WorkingBlockModelElement-form',
'enableAjaxValidation'=>false,
)); ?>
<?php echo $form->errorSummary($model);?>
<div>
<?php echo $form->labelEx($model,'working_block'); ?>
<?php $kmodel = WorkingBlock::model()->findAll(array('order' => 'name'));
echo $form->dropDownList($model, 'working_block', CHtml::listData($kmodel, 'id', 'name'),array('prompt'=>'please select...',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Display/dynamicwbmwbe'), //url to call.
'update'=>'#'.CHtml::activeId($model,'working_block_model_elements')
))); ?>
<?php echo $form->error($model,'working_block'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'working_block_model_elements'); ?>
<?php echo $form->listBox($model,'working_block_model_elements',$kjk,$htmlOptions=array('multiple'=>true,'size'=><img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />);
?>
<?php echo $form->error($model,'working_block_model_elements'); ?>
</div>
... more fields that need to be validated ...
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
Here is my controller ajax routine:
public function actionDynamicwbmwbe()
{
$kj=$_SERVER['HTTP_REFERER'];
$kp="";
$where=strrpos($kj,'/');
if ($where!==NULL)
$kp=substr($kj,$where+1);
$kp.='Display';
$kk=$_POST[$kp]['working_block'];
$connection = Yii::app()->db;
$sql3='SELECT DISTINCT weather_station.id, weather_station.name, weather_element.id AS id2, weather_element.abbrev, weather_element.units FROM weather_element, working_block_model, working_block_model_input_element, weather_station WHERE working_block_model.working_block_id = '.$kk.' ';
$sql3.='AND working_block_model_input_element.weather_element_id = weather_element.id AND working_block_model.id = working_block_model_input_element.working_block_model_id AND weather_station.id = working_block_model_input_element.weather_station_id ';
$sql3.='ORDER BY weather_station.name ASC, weather_element.abbrev ASC;';
$rows = $connection->createCommand($sql3)->queryall();
foreach ($rows as $row)
{
$data[$arrcnt]['id']='2,'.$row['id'].','.$row['id2'].',';
$data[$arrcnt]['name']=$row['name'].'-'.$row['abbrev'];
if ($row['units'] != "")
{
$data[$arrcnt]['name'].='-'.$row['units'];
}
$data[$arrcnt]['id'].=$data[$arrcnt]['name'];
$arrcnt++;
}
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),$name,true);
}
}
Any help or insight would be greatly appreciated! Both yii and jquery are new to me, so I am really floundering here. Thanks so much!