yii 验证返回错误时, AJAX 加载的数据消失

c0vxltue  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(110)

我有表单内容名字,姓氏...等和两个droppdownlist国家和城市,我加载城市时国家droppdownlist由 AJAX 更改,一切都很酷.我的问题:
1-当点击提交时,在验证中出现错误,城市值将被替换为空值如何修复?
2-如何使用$form-〉与它我的意思是城市drowpdownlist。

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

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

查看方式:

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

w8rqjzmb1#

如果你看这条线
如果您有一个数组,那么您就可以在数组中找到一个值。
很明显,该列表是空的,如果您以后要编辑内容,它也将是空的。
我建议使用类似于
如果您是一个城市的话,那么您可以在这里找到一个空的城市。如果您的国家/地区是一个国家/地区,那么您的国家/地区可以通过以下方式进行查询:
在这里我不知道你是否应该使用$model->country$model->country_id,这取决于你的模型。

相关问题