Last active
February 10, 2016 12:54
-
-
Save AKovalevich/6a2f7f642ae5918d7d81 to your computer and use it in GitHub Desktop.
Drupal js filesize validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
var mytheme = mytheme || {}; | |
/** | |
* Client-side file input validation of filesize. | |
*/ | |
mytheme.validateFileSize = function (event) { | |
// Define variables. | |
var filesize = event.data.size; | |
var error = Drupal.t('The file size exceeding the maximum file size'); | |
var $this = $(this); | |
var filesize_exceeds = false; | |
// Check uploaded files if they are exists and filesize is defined. | |
if (!navigator.userAgent.match(/MSIE\s(?!10.0)/)) { | |
if (filesize != undefined && this.value.length > 0 && this.files.length > 0) { | |
var $this_files = this; | |
$.each(this.files, function (ind, val) { | |
var current_filesize = val.size; | |
// Check filesize of uploaded file. | |
if (current_filesize > filesize) { | |
// Remove old errors and display the new one. | |
$('.file-upload-js-error').remove(); | |
$('.file-upload-js-error-size').remove(); | |
$this.closest('div.form-managed-file').prepend('<div class="messages error file-upload-js-error-size" aria-live="polite">' + error + '</div>'); | |
$this_files.value = ''; | |
filesize_exceeds = true; | |
return false; | |
} | |
}); | |
} | |
// Remove error if filesize is ok. | |
if (!filesize_exceeds) { | |
$('.file-upload-js-error-size').remove(); | |
} | |
} | |
}; | |
/** | |
* Attach behaviors to managed file element upload fields. | |
*/ | |
Drupal.behaviors.cnamtsFileValidateAutoAttach = { | |
attach: function (context, settings) { | |
// Bind filesize validation. | |
if (settings.file && settings.file.size) { | |
$.each(settings.file.size, function (selector) { | |
var size = settings.file.size[selector]; | |
$(selector, context).once().bind('change', {size: size}, mytheme.validateFileSize); | |
}); | |
} | |
}, | |
detach: function (context, settings) { | |
// Unbind filesize validation. | |
if (settings.file && settings.file.size) { | |
$.each(settings.file.size, function (selector) { | |
$(selector, context).unbind('change', mytheme.validateFileSize); | |
}); | |
} | |
} | |
}; | |
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implements hook_element_info_alter(). | |
*/ | |
function mymodule_element_info_alter(&$type) { | |
if (!empty($type['managed_file'])) { | |
$type['managed_file']['#process'][] = 'mymodule_managed_file_process'; | |
} | |
} | |
/** | |
* Process function to extends the managed_file element type. | |
*/ | |
function mymodule_managed_file_process($element, &$form_state, $form) { | |
// Add the field size to the page as JavaScript settings. | |
if (isset($element['#upload_validators']['file_validate_size'][0])) { | |
$file_size = $element['#upload_validators']['file_validate_size'][0]; | |
$element['upload']['#attached']['js'][0]['data']['file']['size'] = array('#' . $element['#id'] => $file_size); | |
} | |
return $element; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment