Updating Password

Hello: I have this code for the form of updating my user:

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(



)); ?>

	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<?php echo $form->errorSummary($model); ?>

	<div class="row">

		<?php echo $form->labelEx($model,'Old password'); ?>

			<?php echo $form->passwordField($model,'password',array('maxlength'=>100, 'value'=>"")); ?>

		<?php echo $form->error($model,'password'); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'New password'); ?>	

		<?php echo $form->passwordField($model,'new_password',array('maxlength'=>100)); ?>

		<?php echo $form->error($model,'new_password'); ?>



		<?php echo $form->labelEx($model,'Confirm password'); ?>	

		<?php echo $form->passwordField($model,'repeat_password',array('maxlength'=>100)); ?>

		<?php echo $form->error($model,'repeat_password'); ?>




	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>


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

</div><!-- form -->

Which will display a old password, new password and confirm pass.

I would like to make a function which will hash the passwords like my beforeSave(), then compare the old password to my old one (which is hashed). If they matched. it will update to the new password. How can I do this?

To make it simple you should use an ActiveRecord class, ex: User, after validate matching between old pass and new pass (manually or create new model class, ex: UpdatePassForm), set the new password and save it

$old_pass = $_POST['password'];

$new_pass = $_POST['new_password'];

$user = User::model()->findByPk($user_id);

if (md5($old_pass) === $user->password) {

   $user->password = md5($new_password);

   if ($user->save()) {

       // do something



else {

   // do something