如何在yii2 basic中用数据库更改密码?

时间:2016-04-29 06:08:59

标签: php yii2-basic-app

我有以下代码,新密码无法存储在数据库中而不是旧密码。

我的模特

public function rules(){
            return [
                [['oldpass','newpass','repeatnewpass'],'required'],
                ['oldpass','findPasswords'],
                ['repeatnewpass','compare','compareAttribute'=>'newpass'],
            ];`
        }

        public function findPasswords($attribute, $params){
            $user = Employee::find()->where([
                'Employee_email_id'=>Yii::$app->user->identity->Employee_email_id
            ])->one();
            $password = $user->Password;
            if($password!=$this->oldpass)
                $this->addError($attribute,'Old password is incorrect');
        }

        public function attributeLabels(){
            return [
                'oldpass'=>'Old Password',
                'newpass'=>'New Password',
                'repeatnewpass'=>'Repeat New Password',
            ];
        }

控制器

public function actionChangepassword(){
        $model = new PasswordForm;
        $modeluser = Employee::find()->where([
            'Employee_email_id'=>Yii::$app->user->identity->Employee_email_id
        ])->one();

        if($model->load(Yii::$app->request->post())){
            if($model->validate()){
                try{
                    $modeluser->Password = $_POST['PasswordForm']['newpass'];
                    if($modeluser->save()){
                        Yii::$app->getSession()->setFlash(
                            'success','Password changed'
                        );
                        return $this->redirect(['index']);
                    }else{
                        Yii::$app->getSession()->setFlash(
                            'error','Password not changed'
                        );
                        return $this->redirect(['index']);
                    }
                }catch(Exception $e){
                    Yii::$app->getSession()->setFlash(
                        'error',"{$e->getMessage()}"
                    );
                    return $this->render('changepassword',[
                        'model'=>$model
                    ]);
                }
            }else{
                return $this->render('changepassword',[
                    'model'=>$model
                ]);
            }
        }else{
            return $this->render('changepassword',[
                'model'=>$model
            ]);
        }
    }

查看文件

 <p>Please fill out the following fields to change password :</p>

    <?php $form = ActiveForm::begin([
        'id'=>'changepassword-form',
        'options'=>['class'=>'form-horizontal'],
        'fieldConfig'=>[
            'template'=>"{label}\n<div class=\"col-lg-3\">
                        {input}</div>\n<div class=\"col-lg-5\">
                        {error}</div>",
            'labelOptions'=>['class'=>'col-lg-2 control-label'],
        ],
    ]); ?>
        <?= $form->field($model,'oldpass',['inputOptions'=>[
            'placeholder'=>'Old Password'
        ]])->passwordInput() ?>

        <?= $form->field($model,'newpass',['inputOptions'=>[
            'placeholder'=>'New Password'
        ]])->passwordInput() ?>

        <?= $form->field($model,'repeatnewpass',['inputOptions'=>[
            'placeholder'=>'Repeat New Password'
        ]])->passwordInput() ?>

        <div class="form-group">
            <div class="col-lg-offset-2 col-lg-11">
                <?= Html::submitButton('Change password',[
                    'class'=>'btn btn-primary'
                ]) ?>
            </div>
        </div>
    <?php ActiveForm::end(); ?>
</div>

我如何将新密码存储在数据库中? 这段代码出了什么问题?谁能帮我? 提前谢谢。

0 个答案:

没有答案