Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top