Dependant dropdown and edit model mode

I followed the steps in the wiki article (http://www.yiiframework.com/wiki/24/creating-a-dependent-dropdown) to create a dependant dropdown. This is working fine and outputting data via an AJAX call.

The slight issue I have is that in ‘edit’ mode, the dependant dropdown in not populated with any options (obviously because the parent dropdown has not initiated an AJAX call to request the data). The same issue also occurs when the form is re-displayed due to input errors (upon POST).

So, how can I overcome this problem?

Is ajax validation enabled for your form? Can you post some controller and view code?

No I’m not using AJAX Validation. My code is essentially the same as the one in the tutorial, except that I’m using an activeDropDownList, which I have currently set to an empty array because there is no initial data. I know I will need to modify this to make it work in edit mode.

I got it working with (one dropdown of developers populates another of contacts from those companies):

_form:


<?php echo $form->dropDownList( $model, 'dev', $developers, array( 'class' => 'medium-large',

                                                                   'empty' => '',

                                                                   'ajax' => array(

                                                                      'type'   => 'POST',

                                                                      'url'    => CController::createUrl('game/populateformdevcontacts'),

                                                                      'update' => '#Game_dev_contact',

                                                                   ),

                                                             ) );

      ?>

Controller:


/*

    * Functions used to populate form options dynamically

    */

   public function actionPopulateFormDevContacts()

   {

      $contacts = Contact::model()->findAll( array(

                                                'condition' => 'employer=:emp',

                                                'params'    => array( ':emp' => (int)$_POST['Game']['dev'] ),

                                                'order'     => 'name',

                                             ));

      $data = CHtml::listData( $contacts, 'id', 'name' );


      foreach ( $data as $id => $name )

      {

         echo CHtml::tag( 'option', array( 'value' => $id ), CHtml::encode( $name ), true );

      }

   }

Yeah but does that work in edit mode? I.e. when you go to edit the record are the options in the second dropdown already populated?

You should populate the second dropDown according to the input in the first dropDown:




<?php echo CHtml::activeDropDownList($model, 'firstAttribute', ...);?>

<?php echo CHtml::activeDropDownList(

    $model, 

    'secondAttribute', 

      CHtml::listData(

           modelClass::model()->findAll('id= :value', array(':value'=>$model->firstAttribute)

           ...

)))?>



My example does work to have the first dropDown populate the second. The one I posted code for is the first one, upon which the possible values of the second depend.