I have a table "school" and one "ed_center". The school table has an ed_center_id in it. For my views for school, I display the ed_center_name. This is working fine. On my Create/Update form, I use a dropDownList to display the ed_center_names. When I submit the form, nothing gets inserted/updated in the db.
/views/school/_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper; // to create dropDownList
use app\models\EdCenter; // to use relation in dropDownList
// the list of ed centers for dropDownList
$centerList=ArrayHelper::map(edCenter::find()->asArray()->all(), 'ed_center_id', 'ed_center_name');
?>
<div class="school-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'school_name')->textInput(['maxlength' => 40]) ?>
<?= $form->field($model, 'attend_officer')->textInput(['maxlength' => 100]) ?>
<?= $form->field($model, 'attend_email')->textInput(['maxlength' => 100]) ?>
<?= $form->field($model, 'ed_center_id')->dropDownList($centerList,['prompt'=>'--Choose a Center--']) ?>
<?= $form->field($model, 'phone')->textInput(['maxlength' => 12]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
<?= Html::a('Back','index', ['class' => 'btn btn-info']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
The HTML for the dropDownList —
[html]<div class="form-group field-school-ed_center_id">
<label class="control-label" for="school-ed_center_id">Ed Center ID</label>
<select id="school-ed_center_id" class="form-control" name="School[ed_center_id]">
<option value="">–Choose a Center–</option>
<option value="0">UNSET ED CENTER</option>
<option value="1">Columbia-Greene Ed Center</option>
<option value="2">Rensselaer Ed Center</option>
<option value="5" selected>Test Ed Center</option>
<option value="6">College Center</option>
<option value="7">Rensselaer Academy</option>
<option value="8">George Washington School</option>
<option value="9">Teachers Center</option>
<option value="10">District Classrooms</option>
<option value="11">Claverack School</option>
<option value="12">Access Center</option>
<option value="13">Sacket Education Center</option>
<option value="14">Regional Summer School</option>
<option value="15">Special Education</option>
</select>
<div class="help-block"></div>
</div> [/html]