Updated Model Won't Save

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

Might be worth trying to take out the validation rule for pass. It seems a bit odd to have it in there in the first place since pass will most likely never be massively assigned. There might be other things keeping you from saving that model, though. Try to raise your debug level.

I would assume that your fields are empty because there are no validation rules for new_pass and old_pass, so they’re not being massively assigned in your call to loadModel(). As such, when you try to display the form after an error has occurred, those fields have no value assigned. Obviously this is just a guess as you haven’t shown your model rules.

If the above is true, then try adding this at the top of your view to show all model errors:




<?= CHtml::errorSummary($model); ?>



Thanks to both of you,

The problem was that i had added a captcha that is only present on registration. So i needed to set the scenario to apply just to insert and then it works fine with one small hitch.

Despite having a length rule in the model for the pass and new_pass, if I leave the new field blank it updates fine. It I put between 1 - 5 chars in it fails.

Use a required rule for those fields in this specific scenario.

Hi Keith,

Thanks for your help. Before I read your response I had adjusted my rules up like so. So I’m glad your response was similar to what i’d done.




			// new password settings

			array('new_pass', 'length', 'min'=>6, 'max'=>64, 'tooShort'=>'Password is too short'),			

			

			array('new_pass', 'required', 'on' => 'update', 'message'=>'This field can not be blank'),




This stops a blank field being entered which is progress it also continues to stop passwords with lengths 1-5 in length. But If I use a password of 6 or more characters it errors saying ‘Field can not be blank’.

(Although it actually passes clientside validation).

Does this mean the problem is within my controller? I feel like it must be, maybe that I assign the $_POST[‘USer’][‘new_pass’] to $model->pass. Therefore it looking like it’s blank?