You should not pollute your model with garbage. Please always remember these basic principles of MVC:
- Your controller should not know about your model implementation.
- Do not contaminate your model with materials not related to your application business model.
Always create a reusable code, make your code “DRY” (do not repeat yourself)
By the way, what is the purpose of the username field? Since the form will be available only to registered users, the username can be accessed using Yii :: app () → user.
<?php // models/ChangePasswordForm.php class ChangePasswordForm extends CFormModel { /** * @var string */ public $currentPassword; /** * @var string */ public $newPassword; /** * @var string */ public $newPasswordRepeat; /** * Validation rules for this form. * * @return array */ public function rules() { return array( array('currentPassword, newPassword, newPasswordRepeat', 'required'), array('currentPassword', 'validateCurrentPassword', 'message'=>'This is not your password.'), array('newPassword', 'compare', 'compareAttribute'=>'validateNewPassword'), array('newPassword', 'match', 'pattern'=>'/^[a-z0-9_\-]{5,}/i', 'message'=>'Your password does not meet our password complexity policy.'), ); } /** * I don't know your hashing policy, so I assume it simple MD5 hashing method. * * @return string Hashed password */ protected function createPasswordHash($password) { return md5($password); } /** * I don't know how you access user password as well. * * @return string */ protected function getUserPassword() { return Yii::app()->user->password; } /** * Saves the new password. */ public function saveNewPassword() { $user = UserModel::findByPk(Yii::app()->user->username); $user->password = $this->createPasswordHash($this->newPassword); $user->update(); } /** * Validates current password. * * @return bool Is password valid */ public function validateCurrentPassword() { return $this->createPasswordHash($this->currentPassword) == $this->getUserPassword(); } }
Controller action example:
public function actionChangePassword() { $model=new ChangePasswordForm(); if (isset($_POST['ChangePasswordForm'])) { $model->setAttributes($_POST['ChangePasswordForm']); if ($model->validate()) { $model->save();
example template code:
<?php echo CHtml::errorSummary($model); ?> <div class="row"> <?php echo CHtml::activeLabel($model,'currentPassword'); ?> <?php echo CHtml::activePasswordField($model,'currentPassword') ?> </div> <div class="row"> <?php echo CHtml::activeLabel($model,'newPassword'); ?> <?php echo CHtml::activePasswordField($model,'newPassword') ?> </div> <div class="row"> <?php echo CHtml::activeLabel($model,'newPasswordRepeat'); ?> <?php echo CHtml::activePasswordField($model,'newPasswordRepeat') ?> </div> <div class="row submit"> <?php echo CHtml::submitButton('Change password'); ?> </div> <?php echo CHtml::endForm(); ?> </div>
The template should be simple enough to create. This code, with some minor tweaks, is ready to be copied and pasted into another Yii project.