0

我有表单内容名字,姓氏......等等。并拖拽下拉列表国家和城市,当国家下拉列表被ajax更改时,我加载了城市,一切都很酷。我的问题:

1-点击提交时验证出错,城市值将替换为空值如何解决?

2- 如何使用 $form-> 我的意思是 city droppdownlist 。

  • 如果验证没有错误,则表单工作
    工作

  • 单击提交后的表单并在验证中返回错误,例如用户名为空..等
    错误

看法:

        <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>

城市负荷控制器:

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);
    };
}
4

1 回答 1

1

如果你看这条线

echo CHtml::dropDownList('city','', array());

很明显,列表是空的,如果你以后想编辑内容,它也是空的。

我建议使用类似的东西

echo CHtml::dropDownList('city', $model->city, empty($model->country) ? array() : CHtml::listData(Cities::model()->findAllByAttributes(array('country_id' = > $model->country)),'id','city_name_e'));

在这里我不知道您是否应该使用$model->countryor $model->country_id,这取决于您的型号。

于 2012-12-06T09:10:22.027 回答