yii2 How to change user password via actionUpdate?

12,523

Solution 1

You can try write updatePassword function like setPassword with another variable

 public function updatePassword($new_password) {
    $this->password_hash = Yii::$app->security->generatePasswordHash($new_password);
  }

declare a variable

public $new_password;

And add it in rules()

  public function rules() {
    return [
      //...
      ['new_password', 'required'],
      ['new_password', 'string', 'min' => 6],
    ];
  }

And at actionUpdate in your controller add

$model->updatePassword($model->new_password);

This should help

Solution 2

Here "$this" is your Controller which of course, doesn't have 'new_password' property. You'd better not set new password in controller, but do it in model, for example in beforeSave method:

if ($this->new_password) {
    $this->setPassword($this->new_password);
}
Share:
12,523

Related videos on Youtube

Heroicone71
Author by

Heroicone71

Updated on June 04, 2022

Comments

  • Heroicone71
    Heroicone71 almost 2 years

    I have advanced app. I create CRUD for User model. So i got update action. I tried to update password by adding

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

    But it call error, something like "password is write-only variable"

    I tried to use field

    <?= $form->field($model, 'new_password')->passwordInput() ?>
    

    With adding in actionUpdate model->setPassword($this->new_password); and it throw Getting unknown property: common\modules\user\controllers\DefaultController::new_password. But model->setPassword('123456'); successfully setting pussword 123456.

    How can i get new_password field from view, to put it in model->setPassword('there');

    Or maybe exist best way to do it?

    UPD

    I tried do it. Is not work.

      public function beforeSave($insert)
      {
        if (parent::beforeSave($insert)) {
          if ($this->new_password) {
            $this->setPassword($this->new_password);
          }
          return true;
        } else {
          return false;
        }
      }
    

    UPD2

      public function setPassword($password) {
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
      }
    

    And password_hash writing in database. I can easy change hash, via generated CRUD, but don't know how to use setPassword() in updateAction.

  • Heroicone71
    Heroicone71 about 9 years
    Look on update question. I tried and not get success. May be i did it wrong?
  • Pavel Bariev
    Pavel Bariev about 9 years
    Try to debug it and see whether script gets into that "if" statement or not. There may be different reasons: for example, your 'new_password' must be in rules() method - e.g. ['new_password', 'string']. Again we don't know what 'setPassword' method does. Does it return anything, or, I guess, it does something like "$this->password = $password".
  • Heroicone71
    Heroicone71 about 9 years
    I add rules, but it is don't work. Id add setPassword metod in question. I dont know how to debug it.
  • Pavel Bariev
    Pavel Bariev about 9 years
    You may place there something like "die($this->new_password);" and see current value. You could learn how to debug in your IDE too.