Yii admin form drop-downs – with Ajax and other examples

Yii’s admin forms are great way to get a quick start on a project that requires admin tools. It would be great if there was some way to automatically create drop-downs for related tables, especially if they already have foreign keys. Perhaps someone has made an extension, but I haven’t found one.

In any case it’s pretty easy to add a drop down, below are some examples, including an Ajax form that updates another value via Ajax onchange.

Yii drop-down with custom sort:

<div class="row">
  <?php echo $form->labelEx($model,'table_name_id'); ?>
  <?php echo $form->dropDownList($model, 'table_name_id', 
      array('order'=>'t.name_admin ASC')
    ), 'id', 'name_admin'),
    array('prompt'=>'Select Name...')
  ); ?>
  <?php echo CHtml::error($model,'table_name_id'); ?>

with ajax call onchange:
this example changes the entire contents of one drop-down (feature category) based on the language selected:

<div class="row">
  <?php echo $form->labelEx($model,'language_id'); ?>
  <?php echo $form->dropDownList('feature[language_id]', $lang_id,
      array('select'=>'name, id','condition'=>'active = "y"')
    ), 'id', 'name'),
      'ajax' => array(
        'type'=>'POST', //request type
        'url'=>$this->createUrl('feature/herdcats'), //url to call
        'update'=>'#feature_category_id', //selector to update
        //'data'=>'js:javascript statement' 
        //leave out the data key to pass all form values through
        // another option to update or replace (will supersede those):
        // 'success'=>' function(data) { $(\'#feature_category_id\').val(data) }',
      )//,'confirm' => 'foo',//, 'onchange' => 'alert("foo2")',
  ); ?>
  <?php echo $form->error($model,'language_id'); ?>

controller action for above:

	public function actionHerdcats()
		// ajax function to retrieve categories by country:
		$lang_id = (!empty($_POST['feature']['language_id'])) ? $_POST['feature']['language_id'] : DEFAULT_LANG_ID;
		$data = category::model()->findAll('language_id=:parent_id', 
					  array(':parent_id'=>(int) $lang_id));
		$data = CHtml::listData($data,'id','name');
		if (!empty($data)) { echo '<option value="">Select Category...</option>'; }
		foreach($data as $value=>$name) {
			echo CHtml::tag('option',

manual data drop-down:

<?php echo $form->dropDownList($model, 'status', 
  ) ); ?>

check for condition before running action in a controller:

protected function beforeAction($action)
  if (parent::beforeAction($action)) {
    //check GET parameters
    if (in_array($action->id, array('update','delete'))) {
      if (empty($_GET['id']))
        throw new CHttpException(404,'Not found');
     return true;
  } else
    return false;

Leave a Reply