Some JS error refactoring and accordingly updated error rendering when experiment description is too long.

This commit is contained in:
Matej Zrimšek 2016-09-08 12:43:31 +02:00
parent daa144ce7c
commit 6eca3661bf
3 changed files with 23 additions and 24 deletions

View file

@ -61,15 +61,9 @@
var msg = JSON.parse(error.responseText);
renderFormError(e,
form.find("#experiment_project_id"),
msg.message.toString());
msg.message.toString(),
true);
})
.on('submit', function(){
form.clearFormErrors();
});
$(modal).on("hidden.bs.modal", function (){
form.clearFormErrors();
});
}
}
// Setup front-end validations for experiment form
@ -86,25 +80,20 @@
if ( 'name' in msg ) {
renderFormError(e,
element.find("#experiment-name"),
msg.name.toString());
msg.name.toString(),
true);
} else if ( 'description' in msg ) {
renderFormError(e,
element.find("#experiment-description"),
msg.description.toString());
msg.description.toString(),
true);
} else {
renderFormError(e,
element.find("#experiment-name"),
error.statusText);
error.statusText,
true);
}
})
.on('submit', function() {
form.clearFormErrors();
});
$("#new-experiment-modal").on("hidden.bs.modal", function (){
form.clearFormErrors();
});
}
}

View file

@ -33,9 +33,21 @@ $.fn.renderFormErrors = function (modelName, errors, clear, ev) {
* and, if present, mark and show the tab where the error occured and
* focus/scroll to the error input, if it is the first one to be
* specified or if errMsgs is undefined.
*
* @param {string} errAttributes Span element (error) attributes
* @param {boolean} clearErr Set clearErr to true if this is the only
* error that can happen/show.
*/
var renderFormError = function (ev, input, errMsgs, errAttributes) {
var renderFormError = function (ev, input, errMsgs, clearErr, errAttributes) {
clearErr = _.isUndefined(clearErr) ? false : clearErr;
errAttributes = _.isUndefined(errAttributes) ? "" : " " + errAttributes;
$form = $(input).closest("form");
if (!_.isUndefined(errMsgs)) {
if (clearErr) {
$form.clearFormErrors();
}
// Mark error form group
$formGroup = $(input).closest(".form-group");
if (!$formGroup.hasClass("has-error")) {
@ -43,7 +55,6 @@ var renderFormError = function (ev, input, errMsgs, errAttributes) {
}
// Add error message/s
errAttributes = _.isUndefined(errAttributes) ? "" : " " + errAttributes;
error_text = ($.makeArray(errMsgs).map(function (m) {
return m.strToErrorFormat();
})).join("<br />");
@ -51,7 +62,6 @@ var renderFormError = function (ev, input, errMsgs, errAttributes) {
$formGroup.append($errSpan);
}
$form = $(input).closest("form");
$tab = $(input).closest(".tab-pane");
if ($tab.length) {
// Mark error tab

View file

@ -95,7 +95,7 @@ function filesPresentValidator(ev, fileInputs) {
_.each(fileInputs, function(fileInput) {
if (!fileInput.files[0]) {
assetError = I18n.t("general.file.blank");
renderFormError(ev, fileInput, assetError, "data-error='file-missing'");
renderFormError(ev, fileInput, assetError, false, "data-error='file-missing'");
filesPresentValid = false;
}
});
@ -125,7 +125,7 @@ function filesSizeValidator(ev, fileInputs, fileTypeEnum) {
var file = fileInput.files[0];
var assetError = getFileTooBigError(file);
if (assetError) {
renderFormError(ev, fileInput, assetError, "data-error='file-size'");
renderFormError(ev, fileInput, assetError, false, "data-error='file-size'");
filesSizeValid = false;
}
});