afterValidate confusion

In afterValidate method I am encrypting the password for storing in database and it’s working fine. I have created a separate form for changing password with only 2 fields, e.g. password and password_repeat and another form for rest of the user profile. Password change also works fine.

Problem comes when I edit the user profile using the other form. When changes are saved, it goes to afterValidate and again encrypts the password so user can’t log in after editing the profile. I want to ensure that password is encrypted only during registration and password change. It should be left along in case user only edits profile. User profile editing form doesn’t have password fields.

Any help would be highly appreciated.

Try to check in which scenario you are in your afterValidate method and do the encrypting only if it is registration or password change.

CModel.getScenario()

Great tip, thanks kokomo :) I didn’t even think of it when I needed something similar. I ended up testing isNewRecord and/or attribute values :unsure:

Thanks! This is what I was looking for.