scinote-web/app/assets/javascripts/sitewide/form_validators.js.erb
Zmago Devetak 1368716aa0 Merge pull request #130 from ZmagoD/zd_SCI_348
project overview - updating (or creating new) experiment details
2016-09-08 12:49:35 +02:00

153 lines
4.7 KiB
Plaintext

/*
* Form validators. They'll find, render and focus error/s and
* prevent form submission.
*/
/*
* Calls specified validator along with the arguments on form submition.
*/
$.fn.onSubmitValidator = function(validatorCb) {
var params = Array.prototype.slice.call(arguments, 1);
var $form = $(this);
if ($form.length) {
$form.submit(function (ev) {
$form.clearFormErrors();
params.unshift(ev);
validatorCb.apply(this, params);
});
}
};
function textValidator(ev, textInput, canBeBlank, limitLength) {
canBeBlank = (typeof canBeBlank !== 'undefined') ? canBeBlank : false;
limitLength = (typeof limitLength !== 'undefined') ? limitLength : true;
var nameTooShort = $(textInput).val().length === 0;
var nameTooLong = $(textInput).val().length > 50;
var errMsg;
if (!canBeBlank && nameTooShort) {
errMsg = I18n.t("general.text.not_blank");
} else if (limitLength && nameTooLong) {
errMsg = I18n.t("general.text.length_too_long", { max_length: 50 });
}
var noErrors = _.isUndefined(errMsg);
if (!noErrors) {
renderFormError(ev, $(textInput), errMsg);
}
return noErrors;
}
function checklistsValidator(ev, checklists, editMode) {
var noErrors = true;
// For every visible (i.e. not removed) checklist
$(checklists).each(function() {
var $checklist = $(this);
if ($checklist.css('display') != 'none') {
// For every visible (i.e. not removed) ckecklist item
anyChecklistItemFilled = false;
$(" .checklist-item-text", $checklist).each(function() {
var $itemInput = $(this);
var $item = $itemInput.closest("fieldset");
if ($item.css('display') != 'none') {
if ($itemInput.val()) {
var itemNameValid = textValidator(ev, $itemInput);
if (!itemNameValid) {
noErrors = false;
}
anyChecklistItemFilled = true;
} else {
// Remove empty checklist item input
$item.remove();
}
}
})
// In edit mode, checklist's name can't be blank if any items present
var allowBlankChklstName = !(anyChecklistItemFilled || editMode);
var $checklistNameInput = $checklist.find(".checklist_name");
var checklistNameValid = textValidator(ev, $checklistNameInput, allowBlankChklstName);
if (!checklistNameValid) {
noErrors = false;
} else if (allowBlankChklstName) {
// Hide empty checklist (remove would break server-side logic)
$checklist.hide();
}
}
});
return noErrors;
}
var FileTypeEnum = Object.freeze({
FILE: 0,
AVATAR: 1
});
function filesValidator(ev, fileInputs, fileTypeEnum, canBeEmpty) {
canBeEmpty = (typeof canBeEmpty !== 'undefined') ? canBeEmpty : false;
var filesValid = true;
if (fileInputs.length) {
var filesPresentValid = canBeEmpty || filesPresentValidator(ev, fileInputs);
var filesSizeValid = filesSizeValidator(ev, fileInputs, fileTypeEnum);
// File spoof check is done on server-side only
filesValid = filesPresentValid && filesSizeValid;
}
return filesValid;
}
function filesPresentValidator(ev, fileInputs) {
var filesPresentValid = true;
_.each(fileInputs, function(fileInput) {
if (!fileInput.files[0]) {
assetError = I18n.t("general.file.blank");
renderFormError(ev, fileInput, assetError, false, "data-error='file-missing'");
filesPresentValid = false;
}
});
return filesPresentValid;
}
function filesSizeValidator(ev, fileInputs, fileTypeEnum) {
function getFileTooBigError(file) {
if (!file) {
return ;
}
var size = parseInt(file.size);
<% avatarSizerLimit = AVATAR_SIZE_LIMIT %>;
<% fileSizeLimit = FILE_SIZE_LIMIT %>;
if (fileTypeEnum == FileTypeEnum.FILE && size > <%= fileSizeLimit.megabytes %>) {
return "<%= I18n.t 'general.file.size_exceeded', file_size: fileSizeLimit %>".strToErrorFormat();
} else if (fileTypeEnum == FileTypeEnum.AVATAR && size > <%= avatarSizerLimit.megabytes %>) {
return "<%= I18n.t 'general.file.size_exceeded', file_size: avatarSizerLimit %>".strToErrorFormat();
}
};
// Check if any file exceeds allowed size limit
var filesSizeValid = true;
_.each(fileInputs, function(fileInput) {
var file = fileInput.files[0];
var assetError = getFileTooBigError(file);
if (assetError) {
renderFormError(ev, fileInput, assetError, false, "data-error='file-size'");
filesSizeValid = false;
}
});
if(filesSizeValid) {
// Check if there is enough free space for the files
filesSizeValid = enoughSpaceValidator(ev, fileInputs);
}
return filesSizeValid;
}
/*
* Overriden in billing module for checking whether enough
* organization space is free.
*/
function enoughSpaceValidator(ev, fileInputs) {
return true;
}