[SOLVED] password compare not working

Hi All,

I think I’m missing something really obvious here, so if someone could point me in the right direction I’d really appreciate it!

Basically, all I’m trying to do is have a ‘confirm password’ option on my sites registration form.

I have a user class, which has the following within it:




public $password_repeat; // as it's not a database field


...


public function rules()

{

	// NOTE: you should only define rules for those attributes that

	// will receive user inputs.

	return array(

		...

		array('password', 'compare', 'on'=>'registration', 'compareAttribute'=>'password_repeat'),

	);

}


...

Within my view file, it’s simply called within a CActiveForm call like:


...

<tr>

	<td></td> 

	<td align="left">

	<h4><?php echo $form->labelEx($model,'password'); ?></h4>

	<label> 

		<?php echo $form->passwordField($model,'password',array('size'=>15,'maxlength'=>15,'value'=>'')); ?>

<?php echo $form->error($model,'password'); ?>

	  </label></td> 

</tr>

<tr>

	<td></td> 

	<td align="left">

	<h4><?php echo $form->labelEx($model,'password_repeat'); ?></h4>

	<label> 

		<?php echo $form->passwordField($model,'password_repeat',array('size'=>15,'maxlength'=>15,'value'=>'')); ?>

<?php echo $form->error($model,'password_repeat'); ?>

	  </label></td> 

</tr>

...

And finally, I try to validate the form in the controller:


$formuser = new User;

$formuser->setRegistration();

if(isset($_POST['User']))

{

	$formuser->attributes=$_POST['User'];

	if($formuser->validate())

		...



This works fine for everything else, I print_r($formuser) and it’s set all the other registration values, but not password_repeat, so $formuser->validate() returns null (as it looks like ‘password_repeat’ is null, so therefore not the same as ‘password’), and the registration fails giving the error:


Please fix the following input errors:


Password must be repeated exactly.

So the questions is, what have I missed? lol

Thanks!

Stu

That means that the validation works correctly.

I’d check if you are encoding the password (maybe md5) on beforeValidate, you should better do it in beforeSave

Yeah, the validation seems to be working (as there doesn’t seem to be any value for password_repeat), but I’m not sure why it’s not passing a value for password_repeat to the model when I apply the attributes?

My encryption should be run after validation:


protected function afterValidate()

{

	parent::afterValidate();

	$this->password = $this->encrypt($this->password);

}

Didn’t you forget to add “password_repeat” to the rules?

Hi sorftark,

I have this defining password_repeat, I think it’s enough?


array('user, user_reg, user_postal_town, user_postal_county, user_physical_town, user_physical_county, password, password_repeat, email', 'length', 'max'=>50),

No, it’s not, I have to define it as safe don’t I?

I added:


array('password_repeat', 'safe'),

to the rules and it seems to work fine!

Thanks guys!

Graeat that you solved!

I’d advice you to move anyway the encoding of password in beforeSave, so if the form is not valid the password will not be encoded, and the eventually inserted password will not change.

Ah, yes that’s quite clever! Thanks for the advice guys :D