You are viewing revision #5 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
In this wiki I will show how could use a TbActiveForm for changing the old password.
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...
CPasswordHelper
Please use CPasswordHelper in your tutorial
Thanks for suggestions
hey thnaks for your suggestions..
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
RE: #18020
@adij
It's memory related issue find on stackoverflow or CBaseController.
Post your issue on forum so yii members can help you.
Error resolved
Many thanks for helping,
I resolved the error
I just change 'bootstrap.widgets.TbActiveForm' to 'CActiveForm'.
Many thanks
RE: #18026
@adij thanks to use this :)..
TbActiveForm is features Bootstrap extension. Need to use default yii CActiveForm.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.