Thursday, September 12, 2013

Re: How I can edit a user without changing the password?

It is very simple

Step: 1
In edit.ctp add the password input field as like given below:
echo $this->Form->input('password',array('value' => '','autocomplete'=>'off'));

Step 2:
In model User.php set the validation for password as like given below:

'password' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations

Here I set the 'on' attribute value to 'create' so the validation check only at the time of user creation.

Step 3: 
Then add the given codes to Model User.php

public function beforeValidate($options = array()) {
            parent::beforeValidate($options);
            if($this->data['User']['password'] == ""){
                unset($this->data['User']['password']);
            }
            
        }


public function beforeSave($options = array()) {
            if(!empty($this->data['User']['password'])) {
                $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
            } 
            return true;
        }




On Tuesday, 29 May 2012 00:50:13 UTC+5:30, lsri8088 wrote:
Hello,

I'm using cake 2.1 with AuthComponent and standard data modelusers.

How I can edit a user without changing the password?

For example, add a check "I want to change the password." If thischeck is true then I make a hash of the password and keep it inserted.Otherwise I do not modify the password field.
How do I add this check and then check it in my beforeSave() function?

Another option I can think of is to leave the password field empty when I go to edit a user (unset ($ this-> request-> data ['User'] ['password']) ;)and then check if the password is empty or not. But .... when I insert a new user the password field should be mandatory ....

Do you have any ideas?

thanks

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: