I had form content first name , last name …etc . And tow drowpdownlist countries and cites , I loaded cites 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>
Ajax Controller
public function actionDynamiccities() /// Called 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);
};
}
you should be able set a value for your city column in your create action I can point you in the direction only if you could upload all your controller code
It seems to me like what you need to do is look at the $_POST variables to see which dependent dropdown was selected, and pre-render it when the form validation fails so Yii’s default validation errors work. Using this link as an example:
First, refactor out part of the actionDynamiccities() method into a separate public (non-action) method in your controller:
public function getCitiesList($country_id) {
$data=Location::model()->findAll('parent_id=:parent_id',
array(':parent_id'=>(int) $country_id));
return CHtml::listData($data,'id','name');
}
Then fix your refactored AJAX action like so:
public function actionDynamiccities()
{
$data = $this->getCitiesList($_POST['country_id']);
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
Then in your _form.php view file, in the #city_id div where the second dropdown would be AJAXed in by the first, add a little PHP snippet like this:
This way, if you have already submitted the form and the $_POST[‘country_id’] is set, the dependent dropdown is already rendered and it should display the appropriate validation errors.
If the user selects a different country the city dropdown should be replaced like normal still.
I didn’t test this code but it should work in principle. Good luck!