Yii2 - 创建一个活动表单,以便在留空时获取旧数据

时间:2016-05-17 03:29:54

标签: yii2

我尝试制作密码更改功能,通过为用户输入一个表单,然后将其进行哈希并添加到password_hash记录中。为防止使用以前的密码填充,我添加了' value' =>'' 以清除它。

但我有一个问题:如果我将该字段留空为默认状态,则数据库中的password_hash字段也将为空。我该怎么做一个例外,如果这个字段是空白的,那么它会自动获取数据库中的旧值,如果输入了数据,它将取代该值吗?

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

编辑:这是我的控制器文件:

<?php

            namespace app\controllers;

            use app\models\User;
            use Yii;
            use app\models\UserList;
            use app\models\UserlistSearch;
            use yii\web\Controller;
            use yii\web\NotFoundHttpException;
            use yii\filters\VerbFilter;

            /**
             * UserlistController implements the CRUD actions for UserList model.
             */
            class UserlistController extends Controller
            {
                /**
                 * @inheritdoc
                 */
                public function behaviors()
                {
                    return [
                        'verbs' => [
                            'class' => VerbFilter::className(),
                            'actions' => [
                                'delete' => ['POST'],
                            ],
                        ],
                    ];
                }

                /**
                 * Lists all UserList models.
                 * @return mixed
                 */
                public function actionIndex()
                {
                    $searchModel = new UserlistSearch();
                    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

                    return $this->render('index', [
                        'searchModel' => $searchModel,
                        'dataProvider' => $dataProvider,
                    ]);
                }

                /**
                 * Displays a single UserList model.
                 * @param integer $id
                 * @return mixed
                 */
                public function actionView($id)
                {
                    return $this->render('view', [
                        'model' => $this->findModel($id),
                    ]);

                }
                public function actionDomains($id)
                {
                    return $this->render('domains', [
                        'model' => $this->findModel($id),
                    ]);

                }
                /**
                 * Creates a new UserList model.
                 * If creation is successful, the browser will be redirected to the 'view' page.
                 * @return mixed
                 */
                public function actionCreate()
                {
                    $model = new UserList();

                    if ($model->load(Yii::$app->request->post()) && $model->save()) {
                        return $this->redirect(['view', 'id' => $model->id]);
                    } else {
                        return $this->render('create', [
                            'model' => $model,
                        ]);
                    }
                }

                /**
                 * Updates an existing UserList model.
                 * If update is successful, the browser will be redirected to the 'view' page.
                 * @param integer $id
                 * @return mixed
                 */


                public function actionUpdate($id)
                {
                    $model = $this->findModel($id);

                    if ($model->load(Yii::$app->request->post()) ) {
                        $model->password_hash=Yii::$app->getSecurity()->generatePasswordHash($model->password_hash);
                        $model->save();


                        return $this->redirect(['view', 'id' => $model->id]);
                    } else {
                        return $this->render('update', [
                            'model' => $model,
                        ]);
                    }
                }

                /**
                 * Deletes an existing UserList model.
                 * If deletion is successful, the browser will be redirected to the 'index' page.
                 * @param integer $id
                 * @return mixed
                 */
                public function actionDelete($id)
                {
                    $this->findModel($id)->delete();

                    return $this->redirect(['index']);
                }

                /**
                 * Finds the UserList model based on its primary key value.
                 * If the model is not found, a 404 HTTP exception will be thrown.
                 * @param integer $id
                 * @return UserList the loaded model
                 * @throws NotFoundHttpException if the model cannot be found
                 */
                protected function findModel($id)
                {
                    if (($model = UserList::findOne($id)) !== null) {
                        return $model;
                    } else {
                        throw new NotFoundHttpException('The requested page does not exist.');
                    }
                }
            }

1 个答案:

答案 0 :(得分:1)

if(!empty($_POST['UserList']['password']){
---
} 

尝试:

public function actionUpdate($id)
                {
                    $model = $this->findModel($id);

                    if ($model->load(Yii::$app->request->post()) ) {
                       if(!empty($_POST['UserList']['password']){
                        $model->password_hash=Yii::$app->getSecurity()->generatePasswordHash($_POST['UserList']['password']);
                    }
                        $model->save();


                }