user_validate_picture

5 user.module user_validate_picture($file, &$edit, $user)
6 user.module user_validate_picture(&$form, &$form_state)
7 user.module user_validate_picture(&$form, &$form_state)
8 user.module user_validate_picture(&$form, &$form_state)

Validates an image uploaded by a user.

See also

user_account_form()

1 string reference to 'user_validate_picture'

File

modules/user/user.module, line 680
Enables the user registration and login system.

Code

function user_validate_picture(&$form, &$form_state) {
  // If required, validate the uploaded picture.
  $validators = array(
    'file_validate_is_image' => array(), 
    'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')), 
    'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
  );

  // Save the file as a temporary file.
  $file = file_save_upload('picture_upload', $validators);
  if ($file === FALSE) {
    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'))));
  }
  elseif ($file !== NULL) {
    $form_state['values']['picture_upload'] = $file;
  }
}

Comments

Image is replaced during validation and not submission

I made a form that combined the user account and their profile pages, and included a widget to allow the user to change their avatar. If the form failed validation for some other reason (invalid email address, for example) the avatar was still replaced with the uploaded one.

Watch out on this one. Even if your form fails validation and doesn't get submitted, the new avatar is used anyway.

Login or register to post comments