ProudNoob
ProudNoob

Reputation: 77

Yii2 - make an active form to take its old data when left blank

I'm trying to make password changing function, by making a form for user to type in, then it will be hashed and added to password_hash record. To prevent it being populated with previous password, I added 'value'=>'' to clear it out.

But I have a problem with this: if I leave the field blank like its default state, the password_hash field in database will be blank, too. How can I make an exception that if this field is blank, then it will automatically take the old value in database, and if it has data inputted, it will take that value instead?

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

EDIT: This is my controller file:

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

Upvotes: 1

Views: 1521

Answers (1)

Muhammad Shahzad
Muhammad Shahzad

Reputation: 9652

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

Try:

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();


                }

Upvotes: 1

Related Questions