Hi,
I’m having a slight problem with trying to get my model to save an updated password. I think i’m at the point where I can’t see the woods for the trees. This is probably pretty simple but I thought fresh eyes would help me.
form:
<?php $form = $this->beginWidget('GxActiveForm', array(
'id' => 'user-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
));
?>
<div class="row">
<?php echo $form->labelEx($model,'old_password'); ?>
<?php echo $form->passwordField($model, 'old_pass', array('maxlength' => 40)); ?>
<?php echo $form->error($model,'old_pass'); ?>
</div><!-- row -->
<div class="row">
<?php echo $form->labelEx($model,'new_password'); ?>
<?php echo $form->passwordField($model, 'new_pass', array('maxlength' => 40)); ?>
<?php echo $form->error($model,'new_pass'); ?>
</div><!-- row -->
<div class="row buttons"><br />
<?php
echo GxHtml::submitButton($model->isNewRecord ? 'Register' : 'Save', array('class' => 'btn btn'));
</div>
<?php
$this->endWidget();
?>
controller action:
public function actionUpdatePass($id){
// Load model
$model = $this->loadModel($id, 'User');
if (isset($_POST['User'])) {
// If old pass matches $_POST proceed
if(hash_hmac('sha256', $_POST['User']['old_pass'], Yii::app()->params['encryptionKey']) === $model->pass)
{
// Hash new password
$model->pass = hash_hmac('sha256', $_POST['User']['new_pass'], Yii::app()->params['encryptionKey']);
if ($model->save()) {
// Passwords did match
Yii::app()->user->setFlash('success','<strong>Password Changed</strong>');
$this->redirect(array('user/view', 'id' => $model->id, 'first_name'=> strtolower($model->first_name)));
}
} else {
// Passwords didn't match
Yii::app()->user->setFlash('danger','<strong>Incorrect Password</strong> - Please retry');
}
}
$this->render('updatePass', array(
'model' => $model,
));
}
Model
......
// var for changing old password
public $old_pass;
public $new_pass;
public function rules()
{
array('pass', 'length', 'min'=>6, 'max'=>64, 'tooShort'=>'<span class="label label-important">Password is too short (minimum is 6 characters)</span>'),
.....
);
}
It was working a couple of weeks ago, I’m not sure what I did to break it so to speak. When the correct password is input and tried to be changed it just clears the form.
Thanks in advance
Jonny