CPasswordHelper not working

Hi,

If I encrypt the password with md5 works well. However, if I use CPasswordHelper this error appears in the form:

Please fix the following input errors: Password must be repeated exactly.

This code doesn’t work:




	public function actionCreate()

	{

		$model=new User('insert');

		

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

		{

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

			if($model->validate())

			{

				$model->password = CPasswordHelper::hashPassword($model->password);

				$model->repeatPassword = CPasswordHelper::hashPassword($model->repeatPassword);

				if($model->save())

				{

					$this->redirect(array('admin', 'lang' => $_POST['lang']));

				}

			}

		}


        $model->password = null;

		$model->repeatPassword = null;

		

		$this->render('create',array(

			'model'=>$model,

		));

	}

This code works:


	public function actionCreate()

	{

		$model=new User('insert');

		

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

		{

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

			if($model->validate())

			{

				$model->password = md5($model->password);

				$model->repeatPassword = md5($model->repeatPassword);

				if($model->save())

				{

					$this->redirect(array('admin', 'lang' => $_POST['lang']));

				}

			}

		}


        $model->password = null;

		$model->repeatPassword = null;

		

		$this->render('create',array(

			'model'=>$model,

		));

	}

well you are encrypting the password in the wrong place the password are encrypted before you even hit the validation and two different hashes are generated that’s why the validation fails, move your encryption in beforeSave call like so




<?php


class User extends CActiveRecord

{

...

     public function beforeSave()

     {

          $this->password = CPasswordHelper::hashPassword($this->password);

          return parent::beforeSave();

     }


...

}

ok, it worked. Thanks