1

我有带有文件上传元素的表单。此文件(图像)必须是用户图片,但未设置。

形式:

      $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);
    }
  }
}

感谢您的帮助。

4

1 回答 1

1

您应该尝试使用 $form_state 而不是 $form。$form_state 包含提交的值!此外,在 Drupal 7.x 中,您无法通过简单地使用 $form_state['values']['f']['step4']['file_e'] 来获取值!只需做一个echo "<pre>"; print_R($form_state); echo "</pre>";来学习数组的正确结构!

也看看这段代码: http: //ly2.in/hBMxPK

于 2013-04-13T04:35:27.093 回答