v20agarwal
(Varunagarwal Ep)
August 3, 2012, 11:26pm
1
hi all …
in my form i am using 2 dependant drop down menu and i am doing client side validation …when i submit the form with improper fields the page refreshes and the dropdown boxes go to default pages … how can i avoid this…
what i want to remain at the same point after i press submit .
please help .
thanks
abennouna
(Abennouna)
August 4, 2012, 1:29am
2
You should include in your form a test on $_POST array. Let’s say your dependent models are Country and City, and you’re in an Address view:
<div class="row">
<?php echo $form->labelEx($model,'countryId'); ?>
<?php
if(isset($_POST['Address']['countryId']))
echo CHtml::dropDownList('Address[countryId]', $_POST['Address']['countryId'],
CHtml::listData(Country::model()->findAll(), 'id', 'name'),
array('empty' => 'Please select a country »',
'ajax' => array('type'=>'GET',
'url'=>$this->createUrl('country/cities'),
'update'=>'#Address_cityId' // or CHtml::activeId($model, 'cityId')
)
)
);
else
echo CHtml::dropDownList('Address[countryId]', $model->countryId,
CHtml::listData(Country::model()->findAll(), 'id', 'name'),
array('empty' => 'Please select a country »',
'ajax' => array('type'=>'GET',
'url'=>$this->createUrl('country/cities'),
'update'=>'#Address_cityId'
)
)
);
?>
<?php echo $form->error($model,'countryId'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'cityId'); ?>
<?php
if(isset($_POST['Address']['cityId']))
echo CHtml::dropDownList('Address[cityId]', $_POST['Address']['cityId'],
CHtml::listData(City::model()->findAll(), 'id', 'name'),
array('empty' => 'Please select a city »',
);
else // useful in update scenarios
echo CHtml::dropDownList('Address[cityId]', $model->cityId,
CHtml::listData(City::model()->findAll(), 'id', 'name'),
array('empty' => 'Please select a city »',
);
?>
<?php echo $form->error($model,'cityId'); ?>
</div>