Commit 6379070e authored by webchick's avatar webchick

Issue #2179655 by javisr, nod_: JS file validation broken.

parent 2764138a
...@@ -18,27 +18,33 @@ ...@@ -18,27 +18,33 @@
attach: function (context, settings) { attach: function (context, settings) {
var $context = $(context); var $context = $(context);
var validateExtension = Drupal.file.validateExtension; var validateExtension = Drupal.file.validateExtension;
var selector, elements; var elements;
function initFileValidation (selector) {
$context.find(selector)
.once('fileValidate')
.on('change.fileValidate', { extensions: elements[selector] }, Drupal.file.validateExtension);
}
if (settings.file && settings.file.elements) { if (settings.file && settings.file.elements) {
elements = settings.file.elements; elements = settings.file.elements;
for (selector in elements) { Object.keys(elements).forEach(initFileValidation);
if (elements.hasOwnProperty(selector)) {
$context.find(selector).on('change', {extensions: elements[selector]}, validateExtension);
}
}
} }
}, },
detach: function (context, settings) { detach: function (context, settings, trigger) {
var $context = $(context); var $context = $(context);
var validateExtension = Drupal.file.validateExtension; var validateExtension = Drupal.file.validateExtension;
var selector, elements; var elements;
if (settings.file && settings.file.elements) {
function removeFileValidation (selector) {
$context.find(selector)
.removeOnce('fileValidate')
.off('change.fileValidate', Drupal.file.validateExtension);
}
if (trigger === 'unload' && settings.file && settings.file.elements) {
elements = settings.file.elements; elements = settings.file.elements;
for (selector in elements) { Object.keys(elements).forEach(removeFileValidation);
if (elements.hasOwnProperty(selector)) {
$context.find(selector).off('change', validateExtension);
}
}
} }
} }
}; };
...@@ -115,6 +121,8 @@ ...@@ -115,6 +121,8 @@
}); });
$(this).closest('div.form-managed-file').prepend('<div class="messages messages--error file-upload-js-error" aria-live="polite">' + error + '</div>'); $(this).closest('div.form-managed-file').prepend('<div class="messages messages--error file-upload-js-error" aria-live="polite">' + error + '</div>');
this.value = ''; this.value = '';
// Cancel all other change event handlers.
event.stopImmediatePropagation();
} }
} }
}, },
......
...@@ -1266,7 +1266,7 @@ function file_managed_file_process($element, &$form_state, $form) { ...@@ -1266,7 +1266,7 @@ function file_managed_file_process($element, &$form_state, $form) {
$element['upload']['#attached']['js'] = array( $element['upload']['#attached']['js'] = array(
array( array(
'type' => 'setting', 'type' => 'setting',
'data' => array('file' => array('elements' => array('#' . $element['#id'] . '-upload' => $extension_list))) 'data' => array('file' => array('elements' => array('#' . $element['#id'] => $extension_list)))
) )
); );
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment