I’m trying to update only some information for a user. Specifically, I don’t want to change the password. So I have my nice form with the data I want to change and my controller ready to receive that, even specifying the fields in the $model->save($param).
I have the controller ready to validate all stuff including the required password. But I don’t need to validate it, since I’m not changing it. Even so, I get a validation error telling me that the password is required.
I only want to change some specific fields, and I wish to do that in only one query (that leaves $model->saveField() out of the game).
Is this possible?
My controller:
if (!empty($this->data)) {
$this->User->set($this->data);
if ($this->User->save()) {
//do stuff
}
}
The data array only contains the fields sent from the form, which don’t include the password, and still, I get a validation error.
In the model I have the following validation rule:
'passwordConfirmacion' => array(
'camposIdenticos' => array(
'rule' => array('camposIdenticos', 'password'),
'message' => 'La confirmacion no coincide',
'required' => true,
'allowEmpty' => false,
),
function camposIdenticos($data, $comparacion) {
$temp = array_keys($data);
$campo = $temp[0];
return $data[$campo] === $this->data['User'][$comparacion];
}
Add
'on' => 'create'to the validation rule. That way the validation for the password is done only when creating the record.If you want to keep the password validation for some cases later (e.g. when the user actually wants to change their password), add another similar rule but with
'on' => 'update'and'required' => false.