Sunday, November 30, 2008

How to validate and update password of a user with affecting HABTM relations for the user?

I have the following code for my "reset user password" administrator
page:

<div class="users form">
<?php echo $form->create('User',array('action' => 'resetpassword'));?>
<?php echo $html->link('Back',array('action' => 'edit',$id)); ?>
<fieldset>
<legend><?php echo 'Reset password for ' . $username['User']
['username'];?></legend>
<?php
echo $form->input('id',array('value'=>$id));
echo $form->input('password',array('label'=>'New Password'));
echo $form->input('password_confirm',array
('type'=>'password','label'=>'Confirm Password'));
echo $form->submit('Submit');
echo $form->button('Reset',array('type' => 'reset'));
echo $form->end();
?>
</fieldset>
<?php echo $html->link('Back',array('action' => 'edit',$id)); ?>
</div>


...then for the Users controller:

function resetpassword($id = null)
{
if (!$id && empty($this->data)) {
$this->Session->setFlash('Invalid User', true);
}
else if (!empty($this->data)) {
$this->User->id = $this->data['User']['id'];
$this->set('id',$this->data['User']['id']);
$username = $this->User->read('username',$this->data['User']
['id']);
$this->set('username',$username);
if($this->User->save($this->data))
{
$this->Session->setFlash('You have successfully reset the
password.');
}
else
{
$this->Session->setFlash('Failed to reset password.
Please try again.');
}
}
else if (empty($this->data)) {
$username = $this->User->read('username', $id);
$this->set('username',$username);
$this->set('id',$id);
}
}

As you can see in the Users controller, I need to call $this->User-
>save($this->data) so that I can send both 'password' and
'password_confirm' to the User model where the validation is
performed.

My validation rules in the User model is as follows:

var validate = array(
'password' => array
(
array('rule'=>array
('passwordCompare','password_confirm'),'message'=>''),
array('rule'=>array('minLength',4),'message'=>'Password
must have at least 4 characters.'),
array('rule'=>array('notEmpty'),'message'=>'Password
cannot be empty.'),
)
);

...where "passwordCompare" is defined to check if 'password' and
'password_confirm' are equal before saving the field in the DB.

My problem is that "Users" is also connected via an HABTM relation to
a "Roles" table, so, when I call $this->User->save($this->data) above,
my existing HABTM relations are getting deleted. I'm suspecting it's
because $this->data only contains data that was in my "reset" form
(password, and password_confirm), but cake requires that the HABTM
relations should be there too (maybe via hidden fields?). My first
idea to fix this was to perform the validation in the controller then
just use the "saveField" function so that I'm sure that only the
password field is being modified.

Question: Is there a way where I can still implement validation in the
model without affecting the HABTM relations???
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: