There are 2 ways to make the dependent dropdown list in Yii framework 2.0.

  • Using plugin:
  • Original code

In this article, I show you how to do it with original php code.

Create Dropdown

First, You should know how to make dropdown in Yii2, this is the basic code which I copied from the documentation

echo $form->field($model, 'name_field')->dropDownList(


I had 2 models

  • Category
  • Post


use yiihelpersArrayHelper;
$dataCategory=ArrayHelper::map(commonmodelsCategory::find()->asArray()->all(), 'id', 'name');
      echo $form->field($model, 'category_id')->dropDownList($dataCategory,
           ['prompt'=>'-Choose a Category-',
                $.post( "'.Yii::$app->urlManager->createUrl('post/lists?id=').'"+$(this).val(), function( data ) {
                $( "select#title" ).html( data );

      $dataPost=ArrayHelper::map(commonmodelsPost::find()->asArray()->all(), 'id', 'title');
      echo $form->field($model, 'title')

Yii2 has changed Chtml list to  ArrayHelper::map.

use yiihelpersArrayHelper;


I created action List in Post Controller

public function actionLists($id) {
     $countPosts = commonmodelsPost::find()
          ->where(['category_id' => $id])
     $posts = commonmodelsPost::find()
          ->where(['category_id' => $id])
          ->orderBy('id DESC')
     if($countPosts>0) {
          foreach($posts as $post){
               echo "<option value='".$post->id."'>".$post->title."</option>";
          echo "<option>-</option>";