Skip to content

Commit

Permalink
Issue/2086 edit entry doesnt respect file uploads maximum number of f…
Browse files Browse the repository at this point in the history
…iles setting2 (#2132)

- Fixes
#2086 (comment)
- Button is disabled now after maximum files is reached
- Maximum number error message is shown if maximum files is more than 1
- Fixed an extra issue where if you have multi upload file with max 3
files, Then you upload all 3 files in the edit entry page and then after
the edit you remove one file and try to upload a different file, it
doesn't register that new file and shows a message that the maximum is
reached.

💾 [Build
file](https://www.dropbox.com/scl/fi/tg5yljzjyjjritbskillh/gravityview-2.28.0-572e0fa02.zip?rlkey=mkze7xj5om0xnq8010fzd0pt8&dl=1)
(572e0fa).
  • Loading branch information
mrcasual authored Oct 2, 2024
2 parents 3d3d81e + 969e7a4 commit 0a3a26c
Show file tree
Hide file tree
Showing 4 changed files with 494 additions and 353 deletions.
83 changes: 83 additions & 0 deletions assets/js/fe-views.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,95 @@ jQuery( function ( $ ) {
$( '.gv-search-clear' ).on( 'click', this.clear_search );

$( 'a.gv-sort' ).on( 'click', this.multiclick_sort );

this.disable_upload_file_when_limit_reached();

this.fix_updating_files_after_edit();

this.number_range();

this.iframe();
},

/**
* Fix the issue of updating files after edit where the previous value still exists in the uploaded field.
*/
fix_updating_files_after_edit: function(){
$.each($('.ginput_preview_list'), function(index, element){
if ($(element).children().length > 0) {
return true;
}

$(element).parents('form').find('[name=gform_uploaded_files]').val('');
});
},

/**
* Fix the issue for file upload fields where the button is not disabled and show message for multi upload file.
*/
disable_upload_file_when_limit_reached: function(){

var checkUploaders = setInterval(function() {
if (typeof gfMultiFileUploader !== 'undefined' && gfMultiFileUploader.uploaders) {
clearInterval(checkUploaders);
$.each(gfMultiFileUploader.uploaders, function(index, uploader){
uploader.bind('Init', function(up, params) {
var data = up.settings;
var max = data.gf_vars.max_files;
var fieldId = data.multipart_params.field_id;
var existingFilesCount = $('#preview_existing_files_'+fieldId).children().length;
var limitReached = existingFilesCount >= max;
gfMultiFileUploader.toggleDisabled(data, limitReached);
});

uploader.bind('FilesAdded', function(up, files) {
var data = up.settings;
var max = data.gf_vars.max_files;
var fieldId = data.multipart_params.field_id;
var formId = data.multipart_params.form_id;
var newFilesCount = $('#gform_preview_'+formId+'_'+fieldId).children().length;
var existingFilesCount = $('#preview_existing_files_'+fieldId).children().length;
var limitReached = existingFilesCount + newFilesCount >= max;

$.each(files, function(i, file) {
if (max > 0 && existingFilesCount >= max){
up.removeFile(file);
$('#'+file.id).remove();
return;
}

existingFilesCount++;
});

gfMultiFileUploader.toggleDisabled(data, limitReached);


// Only show message if max is greater than 1
if(max <= 1){
return true;
}

// Check if message already exists

if($("#" + up.settings.gf_vars.message_id).children().length > 0){
return true;
}
$( "#" + up.settings.gf_vars.message_id ).prepend( "<li class='gfield_description gfield_validation_message'>" +
$('<div/>').text(gform_gravityforms.strings.max_reached).html()
+
"</li>" );
// Announce errors.
setTimeout(function () {
wp.a11y.speak( $( "#" + up.settings.gf_vars.message_id ).text() );
}, 1000 );

});

});
}
}, 1);
},

/**
* Triggered when the search form changes
* - Adds 'data-form-changed' attribute to <form> wrapper
Expand Down
Loading

0 comments on commit 0a3a26c

Please sign in to comment.