Dependent dropdown list in yii returns empty string even after validation

房东的猫 提交于 2019-12-25 01:37:27

问题


I know this question might be familiar to most of you, but allow me to ask because most of the answers to related questions here in this forum and in YII forum have not been given a satisfactory answer. My two depended drop downs are working fine and the 2nd one gets populated based on the selection of the first one. All values are from the database. Now my problem is that, the second dropdown keeps on returning an empty value even after a value is selected and it doesn't seem to be validated. I have searched through the forums but none of them seem well tackled. Pliz help me out. This very important to me.

EDITED: I am sorry, I had assumed that its sth that u already know but here is my code: _form.php

<div class="row">
    <?php echo $form->labelEx($users,'country_id'); ?>
    <?php echo $form->dropDownList($users,'country_id',CHtml::listData(Country::model()->findAll(
                array('order' => 'country')),'country_id','country'),
                array(
                    'class'=>'form-control',
                    'prompt'=>'Choose Your Country',
                    'ajax'=>array(
                        'type'=>'POST',
                        'url' => CController::createUrl('users/findcity'),
                        'data'=> array('country_id'=>'js:this.value'),
                        'update'=>'#city_id'))
            );?> 
    <?php echo $form->error($users,'country_id'); ?>
</div>

    <div class="row">
    <?php echo $form->labelEx($address,'city_id'); ?>
    <?php echo CHtml::dropDownList('city_id','', array(),
                array('prompt'=>'Choose Your City','class'=>'form-control')); ?>
    <?php echo $form->error($address,'city_id'); ?>
</div>

And here is my controller function that find the cities associated with the choosen country

public function actionFindcity()
    {
        $data = City::model()->findAll('country_id=:country_id',
                                array(':country_id'=>$_POST['country_id']));
        $data=CHtml::listData($data,'city_id','name');

        echo "<option value=''>City</option>";
        foreach($data as $value=>$name)
            echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
    }

I have tried many ways but I cant seem to figure out where the problem is, tho am doubting these line of code coz I cant seem to find the model that responds to this code:

<?php echo CHtml::dropDownList('city_id','', array(),
                array('prompt'=>'Choose Your City','class'=>'form-control')); ?>

Thanks for any help.


回答1:


hers the solution

in my _Form

 <?php  if(isset($_POST['your_Model']['country'])) 
                                {$countryid = $_POST['your_Model']['country'];}
                                else {$countryid = ''; }?>
                        <?php echo $form->dropDownList($model,'country',
                            CHtml::listData(Country::model()->findAll(), 'country_id', 'country'),
                            array(
                            'ajax' => array(
                            'type'=>'POST', //request type
                            'url'=>CController::createUrl('your_Model/dynamicStates'), //url to call.
                            'update'=>'#'.CHtml::activeId($model,'state'),
                             array('class'=>'ajaxlink'),
                            ),'prompt'=>'--select--',)
                             ); ?>

                    </td>
                    <td>
                        <?php echo $form->labelEx($model,'state'); ?>
                    </td>
                    <td>
                         <?php $listStates = array();
                            if(!empty($countryid)) 
                                {
                                    $listStates = $this->getStatesList($countryid);            
                                }   
                            if ($model->state != '') 
                                {
                                    $listStates = $this->getStatesList($model->country);    
                                }
                            ?>
                        <?php echo $form->dropDownList($model,'state',$listStates, array('empty'=>'--select--')); ?> 

and in controller or model

        public function actionDynamicStates(){


    $data = $this->getStatesList($_POST['your_Model']['country']);
    foreach($data as $value=>$name)
    {
        echo CHtml::tag('option',
        array('value'=>$value),CHtml::encode($name),true);
    }
}

public function getStatesList($country_id) {
    $data=State::model()->findAll('country_id=:id',
    array(':id'=>(int)$country_id));
    return CHtml::listData($data,'state_id','state');
}

Hope this Helps You If You want I Can Explain You



来源:https://stackoverflow.com/questions/22132607/dependent-dropdown-list-in-yii-returns-empty-string-even-after-validation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!