1

Insde the User Model

App::uses('SimplePasswordHasher','Controller/Component/Auth');

class User extends AppModel {
public $useTable = 'users';

public $hasMany = array(
    'Reminder' => array(
        'className' => 'Reminder'
        )
    );

public $validate = array(
        'username' => array(
            'alphaNumeric' => array(
                'rule' => 'alphaNumeric',
                'required' => true,
                'message' => 'Alphabets and numbers only'
                ),
            'between' => array(
                'rule' => array('between',5,15),
                'message' => 'Between 5 to 15 characters'
                ),
            'unique' => array(
                'rule' => 'isUnique',
                'message' => 'Username is already taken'
                )
            ),
        'password' => array(
            'between' => array(
                'rule' => array('between',5,10),
                'message' => 'Between 5 to 10',
                ),
            'compareFields' => array(
                'rule' => array('compareFields','confirm_password'),
                'message' => 'Passwords do not match',
                'required' => 'update'
                )
            ),  
        'email' => array(
            'email' => array(
                'rule' => 'email',
                'message' => 'Please enter valid email address'
                ),
            'unique' => array(
                'rule' => 'isUnique',
                'message' => 'Email is already in use'
                ),
            'compareFields' => array(
                'rule' => array('compareFields','confirm_email'),
                'message' => 'Emails do not match',
                'required' => 'update'
                )
            ),
        'timezone' => array(
            'isValid' => array(
                'rule' => 'isValid',
                'message' => 'Please select a timezone'
                )
            )
        );

Inside the User Controller.

        // Changed existing emails
        if(!empty($this->request->data['User']['email'])) {

            echo "Email is set <br/>";

            $this->User->set($this->request->data['User']['email']);

            if($this->User->validates(array('fieldList' => array('email')))) {

                echo "Email is valid <br/>";

                $this->User->id = $this->Session->read('Auth.User.id');
                $this->User->saveField('email',$this->request->data['User']['email'],true);
                $this->Session->setFlash("Settings updated");                   
            }
        }

        // Change existing password
        if(!empty($this->request->data['User']['password'])) {

            echo "Password is set <br/>";

            $this->User->set($this->request->data['User']['password']);

            if($this->User->validates(array('fieldList' => array('password')))) {

                echo "Password is valid <br/>";

                $this->User->id = $this->Session->read('Auth.User.id');
                $this->User->saveField('password', $this->request->data['User']['password'],true);
                $this->Session->setFlash("Password changed");                   
            } 

        }

I know 'saveField()' doesn't do validation automatically so I've set the validation parameter to true, but to no avail.

I've included the models containing the validations.

Any help would be appreciated.

3
  • What does your validation look like? Is it working in other situations?
    – Erebus
    Feb 20, 2014 at 20:41
  • Yes, I have a register page, and the validation works just fine on that, but the setting page updates existing field values inside the model. Feb 20, 2014 at 20:47
  • have you tried to set $this->User->set($this->request->data); instead of $this->request->data['User']['password']; ?
    – Dezigo
    Feb 20, 2014 at 21:05

1 Answer 1

3

Edited:

I actually ment that you had to do the validation like this:

$this->User->id = $this->Session->read('Auth.User.id');
if(!$this->User->saveField('password', 'asdf', true)) {
    echo "Invalid.";
    pr($this->User->invalidFields());
} else {
    echo "Saved!";
}

or optionally, do this:

Just read() the data, overwrite the new password in that array, then $this->User->set() and use $this->User->validates() like you have.

See here.

3
  • $this->User->saveField('password', $this->request->data['User']['password'], true); is the code s/he is using.
    – Erebus
    Feb 20, 2014 at 20:43
  • 1
    Fixed the comment. But yeah, what I meant was to use the validation that way. Not the save itself. If he wants the validation like he has, he needs to go with the second option. Feb 20, 2014 at 23:28
  • I was going to reply with your edited answer, but you beat me to it. Thanks for helping. Feb 20, 2014 at 23:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.