Yii2-创建活动窗体以在保留为空时获取其旧数据

Yii2-创建活动窗体以在保留为空时获取其旧数据,yii2,Yii2,我正在尝试使用密码更改功能,通过创建一个表单供用户输入,然后将其散列并添加到密码散列记录中。为了防止它被以前的密码填充,我添加了'value'=>''来清除它 但我有一个问题:如果我像默认状态一样将字段留空,数据库中的password\u散列字段也将是空的。如果该字段为空,那么它将自动获取数据库中的旧值,如果它输入了数据,它将取而代之,我怎么能例外呢 <?= $form->field($model, 'password')->passwordInput(['value'=>

我正在尝试使用密码更改功能,通过创建一个表单供用户输入,然后将其散列并添加到密码散列记录中。为了防止它被以前的密码填充,我添加了'value'=>''来清除它

但我有一个问题:如果我像默认状态一样将字段留空,数据库中的password\u散列字段也将是空的。如果该字段为空,那么它将自动获取数据库中的旧值,如果它输入了数据,它将取而代之,我怎么能例外呢

<?= $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.');
                    }
                }
            }

尝试:


可能是这个问题的重复:但这个问题是针对yii1的,我正在尝试,它将使用resultshow更新您的控制器代码!此外,如果密码为空,您可以在控制器中设置一个签入,然后不更新数据库。您是否介意发布一些示例,说明如何检查视图中的表单是否为空?它可以工作!非常感谢你。编辑:啊,该死,当字段留空时显示“哈希无效”。很高兴能帮助你。干杯:)很抱歉再次打扰您,当我将字段留空时显示“哈希无效”。它只起过一次作用。编辑:该死,“密码散列”字段是空的。为什么不在表单中使用
password\u hash
?呃,那是密码散列表单,我只是更改了它的名称来伪装它。如果我不输入该表单,它将是空的,因为密码输入(['value'=>'')
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();


                }