When user pictures are enabled, a validation error on the user_profile_form won't prevent the user's avatar from changing if a new one has been uploaded.
How to reproduce:
1. Upload a user avatar.
2. Visit the account edit page, delete your email address, upload a different avatar and submit the form.
Expected behavior:
- Validation errors reported
- User's avatar does not change
Observed behavior:
- Validation errors reported
- User's avatar is set to the new avatar
Possible cause:
I believe this is due to the fact that user_validate_picture replaces the image. To me, it makes more sense for the user's picture to be replaced on disk only when user_profile_form is submitted, rather than on validation. If a form doesn't validate, none of the changes should take effect, no?
Here's the relevant code for user_validate_picture:
// validate the image...
if ($file = file_save_upload('picture_upload', $validators)) {
// Remove the old picture.
if (isset($form_state['values']['_account']->picture) && file_exists($form_state['values']['_account']->picture)) {
file_delete($form_state['values']['_account']->picture);
}
// The image was saved using file_save_upload() and was added to the
// files table as a temporary file. We'll make a copy and let the garbage
// collector delete the original upload.
$info = image_get_info($file->filepath);
$destination = variable_get('user_picture_path', 'pictures') .'/picture-'. $form['#uid'] .'.'. $info['extension'];
if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
$form_state['values']['picture'] = $file->filepath;
}
else {
form_set_error('picture_upload', t("Failed to upload the picture image; the %directory directory doesn't exist or is not writable.", array('%directory' => variable_get('user_picture_path', 'pictures'))));
}
}
Comments