Dropdownlist字段表单未保存在数据库中

时间:2018-08-22 11:32:05

标签: forms model yii2 dropdown yii2-advanced-app

我是Yii2 Framework的新朋友。我有一个问题,我正在尝试制作注册表单,但是注册后,该角色未保存到数据库。 这是表格。

Form visualization

这是我的角色表:

DB-Table

这是查看代码,register.php:

<h2>Sign Up : </h2>
                <div class="user-form">
                    <?php $form = ActiveForm::begin(); ?>
                    <?= $form->field($model, 'username')->textInput(['maxlength' => true]) ?>

                    <?= $form->field($model, 'email')->textInput(['maxlength' => true]) ?>

                    <?= $form->field($model, 'password')->passwordInput(['maxlength' => true]) ?>

                    <?= $form->field($model, 'user_kind')->dropdownList(
                        ArrayHelper::map(CtKindUser::find()->all(), 'id','kindUser'), [
                            'prompt' => 'Choose Category',]); ?>

                    <div class="form-group">
                        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-primary' : 'btn btn-primary']) ?>
                    </div>

                    <?php ActiveForm::end(); ?>

这是控制器。

public function actionRegister()
{
    $model = new User();
    $modelLogin = new LoginForm();

    if($modelLogin->load(Yii::$app->request->post())&& $modelLogin->login()){
        return $this->goHome();
    }
    if ($model->load(Yii::$app->request->post())) {
        $model->id = $model->generateRandomString();
        $hash = Yii::$app->getSecurity()->generatePasswordHash($model->password);
        $model->password = $hash;
        $model->activated = 1;
        $model->createdAt = date("Y-m-d").' '.date("h:i:s a");
        $model->user_kind = ''; 

        $model->save();
        return $this->redirect(['/site/index']);

        // return $this->redirect(['/site/index']);
    } else {
        return $this->render('register', [
            'model' => $model,
            'modelLogin' => $modelLogin,
        ]);
    }

这是模型。

public function rules()
    {
        return [
            [['username', 'email', 'password'], 'required'],
            [['activated'], 'integer'],
            [['profile_name'], 'string', 'max' => 250],
            [['id', 'user_kind'], 'string', 'max' => 35],
            [['username', 'email'], 'string', 'max' => 50],
            [['createdAt', 'updatedAt'], 'string', 'max' => 30],
            [['user_kind'], 'string', 'max' => 45],
            [['user_kind'], 'exist', 'skipOnError' => true, 'targetClass' => CtKindUser::className(), 'targetAttribute' => ['user_kind' => 'id']],
        ];
    }

    /**
    * @inheritdoc
    */
    public function attributeLabels()
    {
        return [
        'id' => 'ID',
        'username' => 'Username',
        'profile_name' => '',
        'email' => 'Email',
        'password' => 'Password',
        'category' => 'Category',
        'createdAt' => 'Created At',
        'updatedAt' => 'Updated At',
        'activated' => 'Activated',
        'user_kind' => 'Jenis Pengguna',
        ];
    }

    public function getUserKind()
{
    return $this->hasOne(CtKindUser::className(), ['id' => 'user_kind']);
}

有人可以帮助我。到底是什么问题? 谢谢。

1 个答案:

答案 0 :(得分:0)

检查您如何发送下拉列表:

<?= $form->field($model, 'user_kind')->dropdownList(['1' =>'Event Organizer', '2' =>'Tenant'],['prompt'=>'Select Category'])?>

具有12的值,并在您的控制器上:

if ($model->user_kind =='Event Organizer' || $model->user_kind =='Osjfnas8sjaasnaskfowlkvmssjswT' )

您正在与一些永远不会发送的值进行比较'Event Organizer''Osjfnas8sjaasnaskfowlkvmssjswT'