Error handling

Hello,

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>

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

        <div class="row buttons">

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

	</div>

, 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()

	{

                if(Yii::app()->request->isPostRequest){

                    $model=$this->loadModel();

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

                    $model->setScenario("changePassword");

                   if(!$model->validatePassword($_POST['users']['oldPassword'])){

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

                   }

                }

                else{

                    $model=new users();


                    if(isset($_GET['users']))

                            $model->attributes=$_GET['users'];


                    $this->render('change',array(

                            'model'=>$model,

                    ));

                }

	}



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

        }

thanks!

In users model.