Skip to content

Commit

Permalink
fix[Op#52098]: Invalidate form validity when direct upload is too large
Browse files Browse the repository at this point in the history
See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement/setCustomValidity

On direct upload, client side JQuery overrides the form submission to
the _"direct"_ storage provider. This PR intercepts this submission by
attaching form validity via [`HTMLObjectElement.setCustomValidity()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement/setCustomValidity)

----

Co-authored-by: Eric Schubert <[email protected]>
  • Loading branch information
akabiru and Kharonus committed Apr 29, 2024
1 parent b83cfb2 commit 43dd5e5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def defaults
end

def set_direct_upload_file_name
if params[:filesize].to_i > Setting.attachment_max_size.to_i.kilobytes
render json: { error: I18n.t("activerecord.errors.messages.file_too_large", count: Setting.attachment_max_size) },
status: :unprocessable_entity
return
end

session[:pending_ifc_model_title] = params[:title]
session[:pending_ifc_model_is_default] = params[:isDefault]
end
Expand Down
14 changes: 13 additions & 1 deletion modules/bim/app/views/bim/ifc_models/ifc_models/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ See COPYRIGHT and LICENSE files for more details.
fileName = fileNameField[0].files[0].name;
}

<%# Reset the form validity %>
<%# See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement/setCustomValidity %>
fileNameField[0].setCustomValidity('');

var titleField = jQuery("#bim_ifc_models_ifc_model_title");

<%# When creating a new model, there is no titleField. %>
Expand All @@ -86,12 +90,20 @@ See COPYRIGHT and LICENSE files for more details.
},
data: {
title: title,
isDefault: jQuery("#bim_ifc_models_ifc_model_is_default").is(":checked") ? 1 : 0
isDefault: jQuery("#bim_ifc_models_ifc_model_is_default").is(":checked") ? 1 : 0,
filesize: fileNameField[0].files[0].size
},
statusCode: {
401: function(resp, status, error) {
document.location.reload(); // reload to make user login again
}
},
error: function(jqXHR, ajaxOptions, errorThrown) {
if (jqXHR.status == 422) {
var errorMessage = jqXHR.responseJSON.error;
fileNameField[0].setCustomValidity(errorMessage);
fileNameField[0].reportValidity();
}
}
});
};
Expand Down

0 comments on commit 43dd5e5

Please sign in to comment.