Model Scenario Problem

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) );

	}

...

Try change it


array('username, email', 'unique'),

for it


array('username, email', 'unique', 'on'=>'create, update'),

so he just falls in the validation rule on create and update.

Dear Friend

Check whether the following is working.




 public function actionChangePassword()

{               

    $model = new User('changepassword');

    $user=User::model()->findByPk(Yii::app()->user->id);

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

    {

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


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

          {                            


               if(md5(trim($model->currentpassword))!==$user->password) 

               {

                       Yii::app()->user->setFlash('error', "Incorrect current password..");

                        $this->refresh();

               }


               else  


              {    

              $user->password=md5(trim($model->newpassword));


              if ($user->save()) 

                 {

                  Yii::app()->user->setFlash('success', "Password Changed!");

                  $this->refresh();

                 }

              }                  

           }             

     }


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

}

...






But from what I understand, even the view (form) just having CurrentPassword, NewPassword and cNewPassword fields, the controller tries to "upgrade" all fields, including username, etc.

How do I limit the controller only change the data from the form?

Hi Friends,

Same problem with that code =/

The model is trying to validade all mysql entries.

Dear Friend

Sorry for that.

I have edited my post a bit upfront.I doubt that is anyway helpful.

Kindly try this. slight alteration in method "save’




if ($user->save(true,array('password')) 

          {

               Yii::app()->user->setFlash('success', "Password Changed!");

               $this->refresh();

          }






Thanks! You find the solution!

Hi…

Remember that according this article: http://www.yiiframework.com/doc/guide/1.1/en/form.model

When you have multiple actions in the ‘on’ param, you should use an array

‘on’=>array(‘update’, ‘create’),