Wrong file extension JS error is hidden after an upload has already been attempted (e.g., after adding then removing a file)

Created on 26 February 2011, almost 14 years ago
Updated 15 September 2022, over 2 years ago

If you choose file, upload it and remove, choose another file again ( with invalid extension), then error message about extension is shown and hidden immediately. Bug may be in modules/file/file.js in validateExtension function:

validateExtension: function (event) {
    // Remove any previous errors.
    $('.file-upload-js-error').remove(); // !!!! PROBLEM HERE - this function is called twice, second time with this.value == ''
    // Add client side validation for the input[type=file].
    var extensionPattern = event.data.extensions.replace(/,\s*/g, '|');
    if (extensionPattern.length > 1 && this.value.length > 0) {
      var acceptableMatch = new RegExp('\\.(' + extensionPattern + ')$', 'gi');
      if (!acceptableMatch.test(this.value)) {
        var error = Drupal.t("The selected file %filename cannot be uploaded. Only files with the following extensions are allowed: %extensions.", {
          '%filename': this.value,
          '%extensions': extensionPattern.replace(/\|/g, ', ')
        });
        $(this).parents('div.form-managed-file').prepend('<div class="messages error file-upload-js-error">' + error + '</div>');
        this.value = '';
        return false;
      }
    }
  },

Temporarily I override this function in my custom js-file with this code:

  Drupal.file.validateExtension = function (event) {
    // Remove any previous errors.
    if (this.value) // THIS CONDITION was added
    {
      $('.file-upload-js-error').remove();
      // Add client side validation for the input[type=file].
      var extensionPattern = event.data.extensions.replace(/,\s*/g, '|');
      if (extensionPattern.length > 1 && this.value.length > 0) {
        var acceptableMatch = new RegExp('\\.(' + extensionPattern + ')$', 'gi');
        if (!acceptableMatch.test(this.value)) {
          var error = Drupal.t("The selected file %filename cannot be uploaded. Only files with the following extensions are allowed: %extensions.", {
            '%filename': this.value,
            '%extensions': extensionPattern.replace(/\|/g, ', ')
          });
          $(this).parents('div.form-managed-file').prepend('<div class="messages error file-upload-js-error">' + error + '</div>');
          this.value = '';
          return false;
        }
      }
    }
  }

🐛 Bug report
Status

Needs review

Version

7.0 ⚰️

Component
File module 

Last updated 6 days ago

Created by

🇷🇺Russia bazel-1

Live updates comments and jobs are added and updated live.
  • Needs manual testing

    The change/bugfix cannot be fully demonstrated by automated testing, and thus requires manual testing in a variety of environments.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024