2016-08-11 22:05:23 +08:00
|
|
|
/*
|
|
|
|
* Form validators. They'll find, render and focus error/s and
|
|
|
|
* prevent form submission.
|
|
|
|
*/
|
2016-07-23 00:02:57 +08:00
|
|
|
|
2016-08-11 22:05:23 +08:00
|
|
|
/*
|
|
|
|
* Calls specified validator along with the arguments on form submition.
|
|
|
|
*/
|
2016-08-09 20:39:24 +08:00
|
|
|
$.fn.onSubmitValidator = function(validatorCb) {
|
|
|
|
var params = Array.prototype.slice.call(arguments, 1);
|
|
|
|
var $form = $(this);
|
|
|
|
if ($form.length) {
|
|
|
|
$form.submit(function (ev) {
|
2016-08-11 22:05:23 +08:00
|
|
|
$form.clearFormErrors();
|
2016-08-09 20:39:24 +08:00
|
|
|
params.unshift(ev);
|
|
|
|
validatorCb.apply(this, params);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-16 17:39:37 +08:00
|
|
|
var TextLimitEnum = Object.freeze({
|
|
|
|
OPTIONAL: 0,
|
|
|
|
REQUIRED: 1,
|
|
|
|
NAME_MIN_LENGTH: '<%= NAME_MIN_LENGTH %>',
|
|
|
|
NAME_MAX_LENGTH: '<%= NAME_MAX_LENGTH %>',
|
|
|
|
TEXT_MAX_LENGTH: '<%= TEXT_MAX_LENGTH %>'
|
|
|
|
});
|
|
|
|
|
2016-09-21 21:54:12 +08:00
|
|
|
/*
|
|
|
|
* @param {boolean} clearErr Set clearErr to true if this is the only
|
|
|
|
* error that can happen/show.
|
|
|
|
*/
|
|
|
|
function textValidator(ev, textInput, textLimitEnumMin, textLimitEnumMax, clearErr) {
|
|
|
|
clearErr = _.isUndefined(clearErr) ? false : clearErr;
|
|
|
|
|
2016-09-21 21:35:23 +08:00
|
|
|
var text = $(textInput).val().trim();
|
|
|
|
$(textInput).val(text);
|
|
|
|
var nameTooShort = text.length < textLimitEnumMin;
|
|
|
|
var nameTooLong = text.length > textLimitEnumMax;
|
2016-09-16 17:39:37 +08:00
|
|
|
|
2016-07-22 19:29:01 +08:00
|
|
|
var errMsg;
|
2016-09-16 17:39:37 +08:00
|
|
|
if (nameTooShort) {
|
|
|
|
if (textLimitEnumMin === TextLimitEnum.REQUIRED) {
|
|
|
|
errMsg = I18n.t("general.text.not_blank");
|
|
|
|
} else {
|
|
|
|
errMsg = I18n.t("general.text.length_too_short", { min_length: textLimitEnumMin });
|
|
|
|
}
|
|
|
|
} else if (nameTooLong) {
|
|
|
|
errMsg = I18n.t("general.text.length_too_long", { max_length: textLimitEnumMax });
|
2016-07-22 19:29:01 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 05:05:42 +08:00
|
|
|
var noErrors = _.isUndefined(errMsg);
|
|
|
|
if (!noErrors) {
|
2016-09-21 21:54:12 +08:00
|
|
|
renderFormError(ev, textInput, errMsg, clearErr);
|
2016-07-22 19:29:01 +08:00
|
|
|
}
|
2016-09-07 05:05:42 +08:00
|
|
|
return noErrors;
|
2016-07-22 19:29:01 +08:00
|
|
|
}
|
|
|
|
|
2016-07-23 00:02:57 +08:00
|
|
|
function checklistsValidator(ev, checklists, editMode) {
|
2016-07-22 19:29:01 +08:00
|
|
|
var noErrors = true;
|
|
|
|
// For every visible (i.e. not removed) checklist
|
2016-09-07 05:05:42 +08:00
|
|
|
$(checklists).each(function() {
|
|
|
|
var $checklist = $(this);
|
2016-07-22 19:29:01 +08:00
|
|
|
if ($checklist.css('display') != 'none') {
|
|
|
|
|
2016-09-07 05:05:42 +08:00
|
|
|
// For every visible (i.e. not removed) ckecklist item
|
|
|
|
anyChecklistItemFilled = false;
|
2016-07-22 19:29:01 +08:00
|
|
|
$(" .checklist-item-text", $checklist).each(function() {
|
2016-09-07 05:05:42 +08:00
|
|
|
var $itemInput = $(this);
|
|
|
|
var $item = $itemInput.closest("fieldset");
|
|
|
|
if ($item.css('display') != 'none') {
|
|
|
|
|
|
|
|
if ($itemInput.val()) {
|
2016-09-16 17:39:37 +08:00
|
|
|
var itemNameValid = textValidator(ev, $itemInput, TextLimitEnum.REQUIRED, TextLimitEnum.TEXT_MAX_LENGTH);
|
2016-09-07 05:05:42 +08:00
|
|
|
if (!itemNameValid) {
|
|
|
|
noErrors = false;
|
|
|
|
}
|
|
|
|
anyChecklistItemFilled = true;
|
|
|
|
} else {
|
|
|
|
// Remove empty checklist item input
|
|
|
|
$item.remove();
|
|
|
|
}
|
2016-07-22 19:29:01 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-09-16 17:39:37 +08:00
|
|
|
var $checklistInput = $checklist.find(".checklist_name");
|
2016-09-07 05:05:42 +08:00
|
|
|
// In edit mode, checklist's name can't be blank if any items present
|
|
|
|
var allowBlankChklstName = !(anyChecklistItemFilled || editMode);
|
2016-09-16 17:39:37 +08:00
|
|
|
var textLimitEnumMin = allowBlankChklstName ? TextLimitEnum.OPTIONAL : TextLimitEnum.REQUIRED;
|
|
|
|
var checklistNameValid = textValidator(ev, $checklistInput,
|
|
|
|
textLimitEnumMin, TextLimitEnum.TEXT_MAX_LENGTH);
|
2016-09-07 05:05:42 +08:00
|
|
|
if (!checklistNameValid) {
|
|
|
|
noErrors = false;
|
|
|
|
} else if (allowBlankChklstName) {
|
|
|
|
// Hide empty checklist (remove would break server-side logic)
|
|
|
|
$checklist.hide();
|
2016-07-22 19:29:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return noErrors;
|
|
|
|
}
|
|
|
|
|
2016-09-16 17:39:37 +08:00
|
|
|
var FileTypeSizeEnum = Object.freeze({
|
|
|
|
FILE: '<%= FILE_MAX_SIZE.megabytes %>',
|
|
|
|
AVATAR: '<%= AVATAR_MAX_SIZE.megabytes %>'
|
2016-08-09 20:39:24 +08:00
|
|
|
});
|
2016-07-22 19:29:01 +08:00
|
|
|
|
2016-09-07 05:05:42 +08:00
|
|
|
function filesValidator(ev, fileInputs, fileTypeEnum, canBeEmpty) {
|
|
|
|
canBeEmpty = (typeof canBeEmpty !== 'undefined') ? canBeEmpty : false;
|
2016-08-05 23:00:29 +08:00
|
|
|
var filesValid = true;
|
2016-07-22 19:29:01 +08:00
|
|
|
if (fileInputs.length) {
|
2016-09-07 05:05:42 +08:00
|
|
|
var filesPresentValid = canBeEmpty || filesPresentValidator(ev, fileInputs);
|
2016-08-05 23:00:29 +08:00
|
|
|
var filesSizeValid = filesSizeValidator(ev, fileInputs, fileTypeEnum);
|
2016-08-26 04:26:53 +08:00
|
|
|
// File spoof check is done on server-side only
|
2016-08-05 23:00:29 +08:00
|
|
|
filesValid = filesPresentValid && filesSizeValid;
|
|
|
|
}
|
|
|
|
return filesValid;
|
|
|
|
}
|
2016-07-22 19:29:01 +08:00
|
|
|
|
2016-08-05 23:00:29 +08:00
|
|
|
function filesPresentValidator(ev, fileInputs) {
|
|
|
|
var filesPresentValid = true;
|
|
|
|
_.each(fileInputs, function(fileInput) {
|
|
|
|
if (!fileInput.files[0]) {
|
|
|
|
assetError = I18n.t("general.file.blank");
|
2016-09-08 18:43:31 +08:00
|
|
|
renderFormError(ev, fileInput, assetError, false, "data-error='file-missing'");
|
2016-08-05 23:00:29 +08:00
|
|
|
filesPresentValid = false;
|
2016-07-22 19:29:01 +08:00
|
|
|
}
|
2016-08-05 23:00:29 +08:00
|
|
|
});
|
|
|
|
return filesPresentValid;
|
2016-07-22 19:29:01 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 23:00:29 +08:00
|
|
|
function filesSizeValidator(ev, fileInputs, fileTypeEnum) {
|
2016-07-22 19:29:01 +08:00
|
|
|
|
|
|
|
function getFileTooBigError(file) {
|
|
|
|
if (!file) {
|
|
|
|
return ;
|
|
|
|
}
|
2016-09-16 17:39:37 +08:00
|
|
|
if (file.size > fileTypeEnum) {
|
|
|
|
switch (fileTypeEnum) {
|
|
|
|
case FileTypeSizeEnum.FILE:
|
|
|
|
return "<%= I18n.t 'general.file.size_exceeded', file_size: FILE_MAX_SIZE %>".strToErrorFormat();
|
|
|
|
case FileTypeSizeEnum.AVATAR:
|
|
|
|
return "<%= I18n.t 'general.file.size_exceeded', file_size: AVATAR_MAX_SIZE %>".strToErrorFormat();
|
|
|
|
}
|
2016-07-22 19:29:01 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check if any file exceeds allowed size limit
|
2016-08-05 23:00:29 +08:00
|
|
|
var filesSizeValid = true;
|
2016-07-22 19:29:01 +08:00
|
|
|
_.each(fileInputs, function(fileInput) {
|
|
|
|
var file = fileInput.files[0];
|
|
|
|
var assetError = getFileTooBigError(file);
|
|
|
|
if (assetError) {
|
2016-09-08 18:43:31 +08:00
|
|
|
renderFormError(ev, fileInput, assetError, false, "data-error='file-size'");
|
2016-08-05 23:00:29 +08:00
|
|
|
filesSizeValid = false;
|
2016-07-22 19:29:01 +08:00
|
|
|
}
|
|
|
|
});
|
2016-08-05 23:00:29 +08:00
|
|
|
if(filesSizeValid) {
|
2016-07-22 19:29:01 +08:00
|
|
|
// Check if there is enough free space for the files
|
2016-08-26 04:26:53 +08:00
|
|
|
filesSizeValid = enoughSpaceValidator(ev, fileInputs);
|
2016-07-22 19:29:01 +08:00
|
|
|
}
|
2016-08-05 23:00:29 +08:00
|
|
|
return filesSizeValid;
|
2016-07-22 19:29:01 +08:00
|
|
|
}
|
|
|
|
|
2016-08-11 22:05:23 +08:00
|
|
|
/*
|
|
|
|
* Overriden in billing module for checking whether enough
|
|
|
|
* organization space is free.
|
|
|
|
*/
|
2016-08-26 04:26:53 +08:00
|
|
|
function enoughSpaceValidator(ev, fileInputs) {
|
2016-07-22 19:29:01 +08:00
|
|
|
return true;
|
2016-08-09 20:39:24 +08:00
|
|
|
}
|