I have the following validation code processed for my User Model. It works good when creating a User, however, it does not work when updating. I am aware of 'on' => 'create', but I want the rule to also apply during Edit as well. I am allowing a user to change its username, but if what the user is changing it to is already in Db, the submission should be denied.
var $validate = array(
'username' => array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'message' => 'Usa letras ou numeros como o teu << Usuario >>',
),
'between' => array(
'rule' => array('between' , 5, 15),
'message' => 'O << Usuario >> tens que ter entre 5 a 15 letras/numeros',
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'O << Usuario >> que escolhestes ja foi utilizado. Escolhe um outro.'
)
)
);
Since the user needs to be logged in to reach the Edit page, could a custom validation rule be created to:
- Check If the Submitted Username already belongs to the Auth->User('id') and if it is submit normally
- However, if the username being submitted is different than Auth->User('username'), we should check if no one else has this username.
- And if Auth->User() is NULL, process isUnique as normal as this only apply when creating new User.
Thanks
check