I'm trying to dynamically update a drop down Active form field based on the value of the previous field in Yii2 framework. The first field represents a project instance and the second field should display the list of some attributes related to the previously selected project, all this in a dynamic way.
Those are the form fields:
// project drop down field
<?php echo $form->field($model, 'project_id', ['options' => ['class' => 'form-group form-group-default form-group-default-select2']])->widget(Select2::className(), [
'data' => ArrayHelper::map(Projects::find()->all(), 'id', 'fullName'),
'id' => 'project_id',
'options' => [
'placeholder' => 'Select...',
],
'theme' => Select2::THEME_DEFAULT,
])->label('Projects');
?>
// attributes of a given project drop down field
<?php Pjax::begin(['id' => 'my_pjax']); ?>
<?php echo $form->field($model, 'attribute_id', ['options' => ['class' => 'form-group form-group-default form-group-default-select2']])->widget(Select2::className(), [
'data' => ArrayHelper::map(Attributes::find()->all(), 'id', 'fullName'),
'pluginOptions' => ['allowClear' => true],
'options' => [
'placeholder' => 'Select...',
],
'theme' => Select2::THEME_DEFAULT,
])->label('Attributes'); ?>
<?php Pjax::end(); ?>
The controller that renders the _form view doesn't have any special code, it just renders the form, and there are no JS scripts yet. What's the missing code that sould be added in order to make this work?