Changing user password

Hello All,

I’ve been trying to implement user change of password in my application up to no avail. I’ve gone through several forum topics and tried using their examples but still no result.

[b]Here is my model (changepasswordform.php)

[/b]


<?php


class ChangePasswordForm extends CFormModel

{

  public $currentPassword;

  public $newPassword;

  public $newPassword_repeat;

  private $_user;

  

  public function rules()

  {

    return array(

      array(

        'currentPassword', 'compareCurrentPassword'

      ),

      array(

        'currentPassword, newPassword, newPassword_repeat', 'required',

        'message'=>'Enter your {attribute}.',

      ),

      array(

        'newPassword_repeat', 'compare',

        'compareAttribute'=>'newPassword',

        'message'=>'The new password does not match.',

      ),

      

    );

  }

  

  public function beforeSave()

  {

    $this->_user->password = md5($this->newPassword);

   

    return parent::beforeSave();

  }

  public function compareCurrentPassword($attribute,$params)

  {

    if( md5($this->currentPassword) !== $this->_user->password )

    {

      $this->addError($attribute,'The current password is incorrect');

    }

  }

  

  public function init()

  {

       $this->_user = User::model()->findByAttributes(array('username'=>Yii::app()->user->username));

  }

  

  public function attributeLabels()

  {

    return array(

      'currentPassword'=>'Current Password',

      'newPassword'=>'New Password',

      'newPassword_repeat'=>'Repeat Password',

    );

  }

  

  public function changePassword()

  {

      

    $this->_user->password = $this->newPassword;

    

    if($this->_user->save())

      return true;

    //return false;

  }

  

}

[b]my controller/action

[/b]


public function actionChangePassword()

  {

    $model = new ChangePasswordForm;

    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')

    {

      echo CActiveForm::validate($model);

      Yii::app()->end();

    }


    // collect user input data

    if(isset($_POST['ChangePasswordForm']))

    {

      $model->attributes=$_POST['ChangePasswordForm'];

      // Validar input del usuario y cambiar contraseña.

      if($model->validate() && $model->changePassword())

      {

          echo 'yes'; die();

      }  else {

          var_dump($model->errors); die();

          }

//       Yii::app()->user->setFlash('success', '<strong>Success!</strong> Your password changed successfully.');

//       $this->redirect( $this->action->id );

//      

    }

    // Mostrar formulario de cambio de contraseña.

    $this->render('changePassword',array('model'=>$model));

  

}

and my view ('changepassword.php)


<?php

/* @var $this SiteController */

/* @var $model LoginForm */

/* @var $form CActiveForm  */


$this->pageTitle=Yii::app()->name . ' - Cambiar contraseña.';

?>

<div id='box-logo'>

  <div id='logo-sw-270x60'></div>

</div>

<h2>Change Password</h2>

<?php /** @var BootActiveForm $form */

$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(

    'id'=>'changePassword-form',

    'inlineErrors'=>true,

    'enableClientValidation'=>true,

    'clientOptions'=>array(

      'validateOnSubmit'=>true,

    ),

    'htmlOptions'=>array('class'=>'well'),

)); ?>




<?php echo $form->passwordFieldRow($model, 'currentPassword', array('class'=>'span3','placeholder'=>'Current Password...')); ?>

<?php echo $form->passwordFieldRow($model, 'newPassword', array('class'=>'span3','placeholder'=>'New Password...')); ?>

<?php echo $form->passwordFieldRow($model, 'newPassword_repeat', array('class'=>'span3','placeholder'=>'Repeat new password...')); ?>

</br>

<?php $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit', 'label'=>'Enviar', 'type'=>'primary')); ?>

<?php $this->endWidget(); ?>

<?php 

  $this->widget('bootstrap.widgets.TbAlert', array(

      'block'=>true, // display a larger alert block?

      'fade'=>true, // use transitions?

      'closeText'=>'&times;', // close link text - if set to false, no close link is displayed

      'alerts'=>array( // configurations per alert type

        'success'=>array(

          'block'=>true,

          'fade'=>true,

          'closeText'=>'&times;',

        ), // success, info, warning, error or danger

      ),

    )

  );

?>

now, when i save, i don’t get any errors, the fields are validated but the password field is not updated in my db… the page just refreshes… nothing else

Thanks for your help in advance.

I guess the problem is that you are echoing errors from ChangePasswordForm model but if there is any validation/save error it would be in the user model.

When saving in the chnagePassword function add something like


}  else {

   	var_dump([size=2]$this->_user->getErrors()[/size][size=2]); die();[/size]

}

btw. as you need to save only the password you can use saveAttributes() instead of save() - http://www.yiiframew…tributes-detail