我有带文件上传元素的表单。此文件(图像)必须是用户图片,但未设置。
形式:
$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);
}
}
}
感谢您的帮助。
答案 0 :(得分:1)
您应该尝试使用$ form_state而不是$ form。 $ form_state包含提交的值!此外,使用Drupal 7.x,您无法通过简单地使用$ form_state [&#39;值&#39;] [&#39; f&#39;] [&#39; step4&#39;] [来获取值] &#39; file_e&#39;]!只需做一个echo "<pre>"; print_R($form_state); echo "</pre>";
来学习数组的正确结构!
另请查看此代码:http://ly2.in/hBMxPK