_form.php

<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use app\models\Groups; use yii\helpers\ArrayHelper; ?> <div class="users-form"> <?php $form = ActiveForm::begin(); $groups = Groups::find()->all(); $items = ArrayHelper::map($groups,'id','name'); $options = [ $model->id => ['selected' => true], ] ?> <?= $form->field($model,'id')->textInput() ?> <?= $form->field($model,'login')->textInput() ?> <?= $form->field($model,'password')->textInput() ?> <?= $form->field($model,'email')->textInput() ?> <?php $form->field($model,'group_id')->dropDownList($items,$options) ?> <?php //echo HTML::dropDownList('group_id',null,$items) ?> <?php //echo Html::activeDropDownList($model, 'group_id',$items,$options) ?> <div class="form-group"> <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?> </div> <?php ActiveForm::end(); ?> </div> 

model / users.php

 <?php namespace app\models; use Yii; class Users extends \yii\db\ActiveRecord { public static function tableName() { return 'users'; } public function rules() { return [ [['id', 'login','password','email'], 'required'], [['id'], 'integer'], ['email','email'], [['id'], 'unique'], ]; } } 

UsersController.php

 public function actionCreate() { $model = new Users(); $model->created_at=date('Ym-d'); $model->update_at=date('Ym-d'); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } return $this->render('create', [ 'model' => $model, ]); } 

Already tried different methods, nothing works <?php $form->field($model,'group_id')->dropDownList($items,$options) ?> Stupidly does not work activeDropDownList and DropDownList generates HTML code:

  <select name="group_id"> <option value="1">Admin</option> <option value="2">Moder</option> <option value="3">User</option> </select> 

but when I press the button and the form sends data, for some reason the data itself is not added to the table. I set the group_id NULL in the table and see that all the fields are sending data, except for the list.

  • And when I want to update some data in any record, I can change any field, except group_id - xRomax
  • In the model, add the group_id field to the rules, otherwise this field is ignored. - fedornabilkin

0