我有带有文件上传元素的表单。此文件(图像)必须是用户图片,但未设置。
形式:
$form['f']['step4']['file_e'] = array(
'#title' => t('Image'),
'#type' => 'managed_file',
'#description' => t('The uploaded image will be displayed on this page using the image style choosen below.'),
'#upload_location' => 'public://images/',
);
证实
function fill_up_profile_form_validate($form, &$form_state) {
global $user;
if (isset($form['f']['actions']['submit']['#value']) && $form_state['triggering_element']['#value'] == $form['f']['actions']['submit']['#value']) {
$file = file_load($form_state['values']['f']['step4']['file_e']);
$validators = array(
'file_validate_is_image' => array(),
'file_validate_size' => array(1 * 1024 * 1024),
);
$errors = file_validate($file, $validators);
if (!empty($errors)) {
$message = '';
foreach ($errors as $error)
$message .= $error . '<br/>';
form_set_error('Error!', $message);
} else {
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
$edit['picture'] = $file;
user_save($user, $edit);
}
}
}
感谢您的帮助。