Skip to content

Commit

Permalink
MDL-80689 filepicker: better reporting of incorrect file type
Browse files Browse the repository at this point in the history
Before this change, file type errors were reported like system errors. Now,
they are reported in a plain dialoue, which is more like how it works for
drag-drop upload.

Also, before this, after an upload errors, you were left with a blank
filepicker dialogue. Now, the upload form is redisplayed.
  • Loading branch information
timhunt committed Oct 31, 2024
1 parent 78cbc7c commit db03f2a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
25 changes: 21 additions & 4 deletions repository/filepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,13 +688,30 @@ M.core_filepicker.init = function(Y, options) {
}
// error checking
if (data && data.error) {
Y.use('moodle-core-notification-ajaxexception', function () {
return new M.core.ajaxException(data);
});
this.fpnode.one('.fp-content').setContent('');
if (data.errorcode === 'invalidfiletype') {
// File type errors are not really errors, so report them less scarily.
Y.use('moodle-core-notification-alert', function() {
return new M.core.alert({
title: M.util.get_string('error', 'moodle'),
message: data.error,
});
});
} else {
Y.use('moodle-core-notification-ajaxexception', function() {
return new M.core.ajaxException(data);
});
}
if (args.onerror) {
args.onerror(id, data, p);
} else {
// Don't know what to do, so blank the dialogue to ensure it is not left in an inconsistent state.
// This is not great. The user needs to re-click 'Upload file' to reset the display.
this.fpnode.one('.fp-content').setContent('');
}
return;
} else {
if (data.msg) {
// As far as I can tell, msg will never be set by any PHP code. -- Tim Oct 2024.
scope.print_msg(data.msg, 'info');
}
// cache result if applicable
Expand Down
17 changes: 17 additions & 0 deletions repository/tests/behat/upload_file_type.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@core @core_filepicker @_file_upload
Feature: File type can be validated on upload
While uploading files
As a user
I want the file type to be validated to save me from errors

@javascript
Scenario: File-picker does not break if you upload the wrong file type
Given I am on the "filemanager_hideif_disabledif_form" "core_form > Fixture" page logged in as "admin"
When I click on "Add..." "link"
And I select "Upload a file" repository in file picker
And I set the field "Attachment" to "#dirroot#/lib/form/tests/fixtures/filemanager_hideif_disabledif_form.php"
And I click on "Upload this file" "button" in the "File picker" "dialogue"
Then I should see "Text file filetype cannot be accepted." in the "Error" "dialogue"
And I click on "OK" "button" in the "Error" "dialogue"
And I should see "Attachment" in the "File picker" "dialogue"
And "Upload this file" "button" in the "File picker" "dialogue" should be visible

0 comments on commit db03f2a

Please sign in to comment.