Reset Password Form

hi i am creating reset password form in my application i am stuck in this…

my form




<?php


/* 

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

$error = $model->getErrors();

print_r($error);

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

    echo '<div>'.$_GET['msg'].'</div>';

?>

<form method="post" action="<?php echo Yii::app()->getBaseUrl(true).'/index.php/admin/user/resetpassword'?>" enctype="multipart/form-data">

    <div class="row">

        <label>Old Password</label><input type="text" name="oldpassword" value="<?php echo $model->email; ?>"/><span><?php if(isset($error['email'])) echo $error['email'][0]; ?></span>

    </div> 

    <div class="row">

        <label>New Password</label><input type="password" name="newpassword"/><span><?php if(isset($error['password'])) echo $error['password'][0]; ?></span>

    </div>

    <div class="row">

        <label>Repeat Password</label><input type="text" name="repeatpassword" value="<?php echo $model->employeeid; ?>"/><span><?php if(isset($error['employeeid'])) echo $error['employeeid'][0]; ?></span>

    </div>

    <?php echo CHtml::submitButton($model->isNewRecord ? 'Reset Password' : 'Save'); ?>

</form>



print_r($error) is giving me array




Array ( [email] => Array ( [0] => Email cannot be blank. ) [password] => Array ( [0] => Password cannot be blank. ) [employeeid] => Array ( [0] => Employeeid cannot be blank. ) [designation] => Array ( [0] => Designation cannot be blank. ) [manager] => Array ( [0] => Manager cannot be blank. ) [profilepic] => Array ( [0] => Profilepic cannot be blank. ) )



my controllerfunction




        public function actionResetPassword()

        {

            $model = new User;

            

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

            {

                $model->validate();

                $this->redirect(array('resetpassword','msg'=>'Password successfully changed..'));

            }

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

        }



my rules array()




return array(

array('email, password, employeeid, designation, manager, profilepic', 'required'),


array('email','email'),                      


array('profilepic','file','types'=>'jpg,jpeg,png','allowEmpty'=>true,'on'=>'update'),


array('email,employeeid','unique'), 


array('password,repeatpassword,oldpassword','required','on'=>'resetpassword'),


array('email, password, employeeid, designation, manager, profilepic', 'safe', 'on'=>'search'),

		);




problem is when i am submit form at that time it’s giving error say “email,password,employeeid etc… is required”… it’s not validating oldpassword,newpassword and repeatpassword field at all

i have created three variables inside model class




public $oldpassword;

public $newpassword;

public $repeatpassword;



i don’t to how to receive form values in this variable? and how to perform check validation…

can anybody give me some suggestion

Thanks in advance

You use ‘on’=>‘resetpassword’ in rules but did not set that scenario in your controller before processing the model.

You have to apply scenario of reset password in model.

either $model->scenario=resetpassword;

or

        &#036;model = new User('resetpassword');

can you give me some example code?

i changed my controller $model instance like this




$model = new User('resetpassword');



now i am not getting any error even i submit it blank

hi

are you running v2?

I see you have everything in place but you did not assign the form values to the model you need to change that first.


public function actionResetPassword()

        {

            $model = new User;

            

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

            {

                $model->oldpassword = $_POST['oldpassword']; // make sure this correspond to you form oldpassword field

                $model->newpassword = $_POST['newpassword']; // make sure this correspond to you form newpassword field

                $model->repeatpassword = $_POST['repeatpassword']; // make sure this correspond to you form repeatpassword field


                $model->validate();

                $this->redirect(array('resetpassword','msg'=>'Password successfully changed..'));

            }

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

        }

oh thanks worked like charm buddy… one more question i would like to ask you is "How to check db password with entered old password?" in model or controller?

pretty straight forward add a validation rule

goes in your User model > protected/models/User.php


        

public function rules()

        {

                return array(

                         ... // your other rules


                        // passwordfield to be verified

                        array('oldPassword', 'verifyOldPassword'),

                );

        }


        public function verifyOldPassword($attribute,$params)

        {

                if(!$this->hasErrors())

                {

                        $user=User::model()->findByAttributes(array('password', $this->oldPassword));

                        if( ($user===null) && ($user->oldPassword !== $this->oldPassword) )

                                $this->addError('oldPassword','old password is wrong.');

                }

        }



I did not test this code just double check it

are you reset password when user logged in? if yes means select the user record by using


 $user=findByAttribute(array('username'=>Yii::app()->user->name));



and the compare old password with new one…

Thank you very much for your help

Please follow this wiki tutorial

http://www.yiiframework.com/wiki/718/change-password-with-tbactiveform