Hi,
Im trying do make a ChangePassword Scenario using Users Model, but Im getting that error when trying to validade:
[username] => Array ( [0] => Username "admin" already exists. )
At Change Password view only have CurrentPassword, NewPassword and cNewPassword fields.
User.php (Model):
...
public function rules() {
return array(
array('name, lastname, email, gender, birthday, password, username', 'required', 'on'=>'register'),
array('username, email', 'unique'),
array('currentpassword', 'required', 'on'=>'changepassword'),
array('newpassword', 'required', 'on'=>'changepassword'),
array('cnewpassword', 'compare', 'on'=>'changepassword', 'compareAttribute' => 'newpassword'),
array('newpassword', 'length', 'min' => 5, 'max' => 20),
);
}
...
UserController.php:
...
public function actionChangePassword()
{
$model = new User('changepassword');
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
if($model->validate('changePassword')) {
$model=User::model()->findByPk(Yii::app()->user->id);
if(md5($_POST['User']['currentpassword'])!=trim($model->password)) {
Yii::app()->user->setFlash('error', "Incorrect current password..");
$this->refresh();
}
$model->setAttributes($_POST['User']);
$model->password=md5($_POST['User']['newpassword']);
if ($model->save()) {
Yii::app()->user->setFlash('success', "Password Changed!");
$this->refresh();
}
}
}
$this->render('changepassword', array('model'=> $model) );
}
...