diff --git a/plupload.js b/plupload.js index f8be4d1..4078eeb 100644 --- a/plupload.js +++ b/plupload.js @@ -56,13 +56,28 @@ Drupal.behaviors.plupload = { 'target': $form.attr('target') }; $form.submit(function(e) { - var uploader = $('.plupload-element', this).pluploadQueue(); + var uploader = $('#'+id).pluploadQueue(); - // Only allow the submit to proceed if there are files and they've all - // completed uploading. - // @todo Implement a setting for whether the field is required, rather - // than assuming that all are. - if (uploader.files.length > 0 && uploader.total.uploaded == uploader.files.length) { + // Handle file required setting. + if (uploader.files.length == 0) { + if (!pluploadSettings.required) { + // Plupload's html4 runtime has a bug where it changes the attributes + // of the form to handle the file upload, but then fails to change + // them back after the upload is finished. + for (var attr in originalFormAttributes) { + $form.attr(attr, originalFormAttributes[attr]); + } + return; + } + else { + e.preventDefault(); + var label = $('label.'+id).text(); + alert(Drupal.t('You must at least upload one file to !label.', {'!label': label.substring(0, label.length-2)})); + } + } + + // Check if uploaded is completed + if (uploader.total.uploaded == uploader.files.length) { // Plupload's html4 runtime has a bug where it changes the attributes // of the form to handle the file upload, but then fails to change // them back after the upload is finished. @@ -71,19 +86,14 @@ Drupal.behaviors.plupload = { } return; } - - // If we're here, stop the form submit, and perform logic as appropriate - // to the current upload state. - e.preventDefault(); - if (uploader.files.length == 0) { - alert('You must at least upload one file.'); - } else if (uploader.state == plupload.STARTED) { - alert('Your files are currently being uploaded. Please wait until they are finished before submitting this form.'); + e.preventDefault(); + alert(Drupal.t('Your files are currently being uploaded. Please wait until they are finished before submitting this form.')); } else { var stateChangedHandler = function() { if (uploader.total.uploaded == uploader.files.length) { + e.preventDefault(); // Plupload's html4 runtime has a bug where it changes the // attributes of the form to handle the file upload, but then // fails to change them back after the upload is finished. diff --git a/plupload.module b/plupload.module index a37a62a..814dc80 100644 --- a/plupload.module +++ b/plupload.module @@ -225,6 +225,8 @@ function plupload_element_pre_render($element) { } $element['#description'] = theme('file_upload_help', array('description' => $element['#description'], 'upload_validators' => $element['#upload_validators'])); + $settings['required'] = $element['#required']; + $element['#attached']['js'][] = array( 'type' => 'setting', 'data' => array('plupload' => array($element['#id'] => $settings)),