Yii 1.1: Yii Change Password with use of old and repeat password using bootstrap TbActiveForm

7 followers

In this wiki I will show how could use a TbActiveForm for changing the old password with help of old, new and repeat password features.

In your models (User model)

//Define public variable
 
    public $old_password;
    public $new_password;
    public $repeat_password;
 
 
    //Define the rules for old_password, new_password and repeat_password with changePwd Scenario.
 
    public function rules()
    {
      return array(
        array('old_password, new_password, repeat_password', 'required', 'on' => 'changePwd'),
        array('old_password', 'findPasswords', 'on' => 'changePwd'),
        array('repeat_password', 'compare', 'compareAttribute'=>'new_password', 'on'=>'changePwd'),
      );
    }
 
 
    //matching the old password with your existing password.
    public function findPasswords($attribute, $params)
    {
        $user = User::model()->findByPk(Yii::app()->user->id);
        if ($user->password != md5($this->old_password))
            $this->addError($attribute, 'Old password is incorrect.');
    }

In your controller/action

public function actionChangepassword($id)
 {      
    $model = new User;
 
    $model = User::model()->findByAttributes(array('id'=>$id));
    $model->setScenario('changePwd');
 
 
     if(isset($_POST['User'])){
 
        $model->attributes = $_POST['User'];
        $valid = $model->validate();
 
        if($valid){
 
          $model->password = md5($model->new_password);
 
          if($model->save())
             $this->redirect(array('changepassword','msg'=>'successfully changed password'));
          else
             $this->redirect(array('changepassword','msg'=>'password not changed'));
            }
        }
 
    $this->render('changepassword',array('model'=>$model)); 
 }

In your view file (changepassword.php)

<div class="form">
 
<?php $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
            'id' => 'chnage-password-form',
            'enableClientValidation' => true,
            'htmlOptions' => array('class' => 'well'),
            'clientOptions' => array(
                'validateOnSubmit' => true,
            ),
     ));
?>
 
  <div class="row"> <?php echo $form->labelEx($model,'old_password'); ?> <?php echo $form->passwordField($model,'old_password'); ?> <?php echo $form->error($model,'old_password'); ?> </div>
 
  <div class="row"> <?php echo $form->labelEx($model,'new_password'); ?> <?php echo $form->passwordField($model,'new_password'); ?> <?php echo $form->error($model,'new_password'); ?> </div>
 
  <div class="row"> <?php echo $form->labelEx($model,'repeat_password'); ?> <?php echo $form->passwordField($model,'repeat_password'); ?> <?php echo $form->error($model,'repeat_password'); ?> </div>
 
  <div class="row submit">
    <?php $this->widget('bootstrap.widgets.TbButton', array('buttonType' => 'submit', 'type' => 'primary', 'label' => 'Change password')); ?>
  </div>
  <?php $this->endWidget(); ?>
</div>

It's working fine...

Total 6 comments

#18027 report it
Rohit Suthar at 2014/08/28 02:13am
RE: #18026

@adij thanks to use this :)..

TbActiveForm is features Bootstrap extension. Need to use default yii CActiveForm.

#18026 report it
adij at 2014/08/28 01:46am
Error resolved

Many thanks for helping, I resolved the error I just change 'bootstrap.widgets.TbActiveForm' to 'CActiveForm'. Many thanks

#18024 report it
Rohit Suthar at 2014/08/27 06:57am
RE: #18020

@adij It's memory related issue find on stackoverflow or CBaseController.

Post your issue on forum so yii members can help you.

#18020 report it
adij at 2014/08/27 03:41am
Error when i used this

Hi all,

i get this message "Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 40961 bytes) in C:\wamp\yiiessaie\framework\web\CBaseController.php on line 125"

when i use this can someone help me.

Many thanks

#17520 report it
Rohit Suthar at 2014/06/27 06:40am
Thanks for suggestions

hey thnaks for your suggestions..

#17493 report it
BraindeadBZH at 2014/06/23 08:04am
CPasswordHelper

Please use CPasswordHelper in your tutorial

Leave a comment

Please to leave your comment.

Write new article