【问题标题】:Drupal validation solution when using file upload with other required form field将文件上传与其他必需的表单字段一起使用时的 Drupal 验证解决方案
【发布时间】:2014-07-01 00:16:09
【问题描述】:

我在尝试上传包含其他必填字段的表单中的图片时遇到问题。因此,如果我没有在必填字段中输入任何内容并上传图片,我会丢失上传的图片(在表单验证期间,图片不再存在)。 我无法在 form_state 的任何地方打印它。如何在包含其他所需表单元素的表单内上传文件?如果用户忘记在其他必填字段中输入信息,我不希望用户再次上传图片。

有什么想法吗?

    function form() {
    $form['#attributes'] = array('enctype' => "multipart/form-data");
    //'upload' will be used in file_check_upload()
    $form['upload'] = array('#type' => 'file');
    $form['my_require_field'] = array(
        '#type' => 'textfield',
        '#title' => t('Enter code here'),
        '#default_value' => 1,
        '#size' => 20,
        '#required' => TRUE
      );
    }
    function form_validate() {
     if(!file_check_upload('upload')) {
     form_set_error('upload', 'File missing for upload.');
    }
    }
    function form_submit() {
     $file = file_check_upload('upload');
    }

【问题讨论】:

    标签: drupal drupal-6 drupal-forms drupal-form-validation


    【解决方案1】:

    您应该使用您正在使用 Drupal 7 的 managed_file 类型 ID

     $form['upload'] = array(
          '#type' => 'managed_file',
          '#title' => t('Upload Image'),
          '#default_value' => '',
          '#required' => TRUE,
          '#description' => t("Upload Image description"),
        );
    

    在您的提交处理程序中,您可以编写以下内容:

    // Load the file via file fid.
    $file = file_load($form_state['values']['upload']);
    
    // Change status to permanent and save.
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);
    

    希望这会有所帮助!

    【讨论】:

    • 我正在使用 Drupal 6 :)
    • 啊!那么你应该尝试Upload element 模块。它的工作方式与 D6 中的 managed_file 类型相同。
    【解决方案2】:

    对于 Drupal 8

    开箱即用似乎不支持创建file(不是managed_file必需类型的字段等基本功能。

    需要在formValidate() 方法中为此字段实现自定义验证器。 在 ConfigImportForm.php 文件中可以找到此类功能的一个罕见示例。

    这是处理表单中文件字段设置、所需验证和提交的代码的 sn-p。

    <?php
    
    class YourForm extends FormBase {
    
      public function buildForm(array $form, FormStateInterface $form_state) {
        $form['myfile'] = [
          '#title' => $this->t('Upload myfile'),
          '#type' => 'file',
          // DO NOT PROVILDE '#required' => TRUE or your form will always fail validation!
        ];
      }
    
      public function validateForm(array &$form, FormStateInterface $form_state) {
        $all_files = $this->getRequest()->files->get('files', []);
        if (!empty($all_files['myfile'])) {
          $file_upload = $all_files['myfile'];
          if ($file_upload->isValid()) {
            $form_state->setValue('myfile', $file_upload->getRealPath());
            return;
          }
        }
    
        $form_state->setErrorByName('myfile', $this->t('The file could not be uploaded.'));
      }
    
      public function submitForm(array &$form, FormStateInterface $form_state) {
        // Add validator for your file type etc.
        $validators = ['file_validate_extensions' => ['csv']];
        $file = file_save_upload('myfile', $validators, FALSE, 0);
        if (!$file) {
          return;
        }
    
        // The rest of submission processing.
        // ...
      }
    }
    

    来自https://api.drupal.org/comment/63172#comment-63172

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-12
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多