Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35963

cakephp update more field unique

I have a site developed in cakephp. I have a model called User like this:

class User extends AppModel {
    public $name = 'User';

    public $validate = array(
        'username' => array(
            'not_empty' => array(
                'rule'=> 'notEmpty',
                'message'=> 'Username not empty'    
            )
        ),
        'email' => array(
            'email_invalid' => array(
                'rule' => 'email',
                'message' => 'Invalid mail'
            ),
            'email_unique' => array(
                'rule' => 'isUnique',
                'message' => 'Mail already exist inside database'
            )
        )
    );


    public function beforeSave(){
           if (isset($this->data['User']['password'])){
            $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
        }
    }   
}

Into my validate I have the rules email_unique that check if inside the database is already present another email equal.

When I update a user I make this inside my controller:

$this->User->id = $this->request->data['User']['id'];
if ($this->User->save($this->request->data)) {
    $this->redirect (array ('action'=>'index'));
}
else{
    $this->Session->write('flash_element','error');
    $this->Session->setFlash ('Error');
}

It always fail because email isn't unique but is the same record!

I would like to know what is the best method to escape the validation if the save is an update not a create? Or something like: check if the page is edit escape validation or I don't know.. maybe there are many system, I would like to know what is the more correct for my problem.

Thanks

Upvotes: 0

Views: 693

Answers (1)

Oldskool
Oldskool

Reputation: 34837

You can adjust your validation rules to only apply when a new record is created, not when an existing record is updated. You can do this by setting the on key in your validation rule to create, so it will look like this:

'email_unique' => array(
    'rule' => 'isUnique',
    'message' => 'Mail already exist inside database',
    'on' => 'create' // Only apply this rule upon creation of a new record
)

See the documentation on this for further details.

If you also want to block duplicate e-mails upon updating, create a beforeSave method in your User model, looking for the e-mail address:

public function beforeSave($options = array()) {
    // If the email key is set in the data to be saved...
    if (isset($this->data[$this->alias]['email'])) {
        // Make sure the email is not already in use by another user
        if ($this->find('count', array(
            'conditions' => array(
                $this->alias . '.id !=' => $this->data[$this->alias]['id'],
                $this->alias . '.email' => $this->data[$this->alias]['email']
            )
        )) > 0) {
          // The email is found for a user with another id, abort!
          return false;
        }
    }
}

Upvotes: 2

Related Questions