I have run into a bizarre issue with the username being validated (as the title says) in the edit view which renders me unable to save the form. I do not have an input in this view for the username, so why is this this being validated (and failing)?
Validation is as follows:
var $validate = array(
'username' => array(
'empty' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Username is required',
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This username has already been taken'
),
'pattern' => array(
'rule' => array('custom','/[a-zA-Z0-9\_\-]{6,}$/i'),
'message' => 'Must be 6 characters or longer with no spaces',
),
'length' => array(
'rule' => array('maxLength', 15),
'message' => 'Please keep username under 15 characters',
),
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Username cannot be empty',
)
);
the edit function below:
function edit($id = null) {
$this->set('title_for_layout', 'Edit Profile');
unset($this->User->validate['email']['email']);
unset($this->User->validate['username']);
if($this->Auth->user('id')==$id) {
$this->set('user', $this->User->read(null, $id));
} else {
$this->Session->setFlash(__('You are not authorised to edit other member profiles', true));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('Your profile has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('Your profile could not be saved. Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->User->read(null, $id);
}
$groups = $this->User->Group->find('list');
$this->set(compact('groups'));
}
I have unset the username which then only allows me to save the form, but this cannot be the correct method for doing this. Could anyone shed some light on this?
Many thanks!
The
required => trueis making the ‘username’ key a required field, meaning if you just try to update settings or something without the username key, it will invalidate.I’ve not done it myself, but try adding
'on' => 'create'to theemptyrule so it only applies when the record is being created.