diff --git a/repository/filepicker.js b/repository/filepicker.js index 5b703bade0364..a13248961e5e7 100644 --- a/repository/filepicker.js +++ b/repository/filepicker.js @@ -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 diff --git a/repository/tests/behat/upload_file_type.feature b/repository/tests/behat/upload_file_type.feature new file mode 100644 index 0000000000000..46c807ecf5256 --- /dev/null +++ b/repository/tests/behat/upload_file_type.feature @@ -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