Error handling


i’m trying to create a method that would allow my users to change their profile password.

this is the code of the changePassord view:

  <p class="note">Fields with <span class="required">*</span> are required.</p>

        <?php $model->setScenario("changePassword");?>

	<?php echo $form->errorSummary($model); ?>

        <?php echo $form->hiddenField($model,'id_users',array('value'=>Yii::app()->user->id)); ?>

        <div class="row">

		<?php echo $form->labelEx($model,'oldPassword'); ?>

		<?php echo $form->passwordField($model,'oldPassword',array('size'=>40,'maxlength'=>40)); ?>

		<?php echo $form->error($model,'oldPassword'); ?>


        <div class="row">

		<?php echo $form->labelEx($model,'newPassword'); ?>

		<?php echo $form->passwordField($model,'newPassword',array('size'=>40,'maxlength'=>40)); ?>

		<?php echo $form->error($model,'newPassword'); ?>


        <div class="row buttons">

		<?php echo CHtml::submitButton('Save'); ?>


, in users model i added variables newPassowrd, oldPassword, and this rule:

array('oldPassword, newPassword', 'required', 'on'=>'changePassword'),

Then in usersController i added this function:

public function actionChangePassword()







                        $this->redirect(array('changePassword', 'id'=>Yii::app()->user->id));




                    $model=new users();








i have two questions:

1: why the form of changePassword.php doesn’t retrieve any error, even if both fields are empty?

2: in actionChangePassword i’d like to validate oldPassword; if the user inserts an incorrect password, i’d like to redirect him to the changePassword page (i already do it) adding an error, something like “please inser the correct old password!”.

thanks a lot! :)

you should insert $this->addErrors() in your models.

paste your method of “PasswordValidate” if you can’t find the problem!

thanks for the reply…

Sorry, i should add $this->addErrors() in users model, or in usersController in actionChangePassword?

the method passwordValidate is quite simple:

public function validatePassword($password)


            return($password === $this->password);



In users model.