I had form content first name, last name …..etc. And tow drowpdownlist countries and cities, I loaded cities when Country drowpdownlist changed by ajax, and everything is cool.
My problems:
1- when clicked submit and had error in validation, city value will replaced with empty value
How to fix it?
2- How to use $form-> with it I mean city drowpdownlist .
-
Form work if no error in validation

-
Form after clicked on submit and return error in validation like username is empty ..etc

View:
<div class="row">
<?php echo $form->labelEx($model,'البلد'); ?>
<?php
/*
echo chtml::activeDropDownList($model,'country',$model->getcountry(),array('prompt'=>'اختر بلدك ') ,
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('current/dynamiccities'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#city', //selector to update
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
))
);
*/
echo CHtml::activedropDownList($model,'country',$model->getcountry(),
array(
'prompt'=>'اختر البلد أو المنطقة ',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('register/dynamiccities'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#city', //selector to update
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
))
);
///
////
?>
<?php echo $form->error($model,'country'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'المدينة'); ?>
<?php
echo CHtml::dropDownList('city','', array());
?>
<?php echo $form->error($model,'city'); ?>
</div>
controller for cities load:
public function actionDynamiccities() /// Call Ajax
{
$country=intval($_POST['Users']['country']);
$data=Cities::model()->findAll('country_id=:country_id',
array(':country_id'=>$country));
$data=CHtml::listData($data,'id','city_name_e');
echo CHtml::tag('option',
array('value'=>''),CHtml::encode('Select Your City '),true);
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
};
}
If you look at this line
It is obvious that the list is empty, and it would also be empty if you wanted to edit the content later.
I would suggest using something like
Here i don’t know if you should use
$model->countryor$model->country_id, depends on your model.