Upgrade to v26 (#172)
* A bunch of new updates from icehrm pro * Push changes to frontend
This commit is contained in:
@@ -1,280 +1,246 @@
|
||||
/*
|
||||
This file is part of Ice Framework.
|
||||
|
||||
Ice Framework is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Ice Framework is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Ice Framework. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
------------------------------------------------------------------
|
||||
|
||||
Original work Copyright (c) 2012 [Gamonoid Media Pvt. Ltd]
|
||||
Developer: Thilina Hasantha (thilina.hasantha[at]gmail.com / facebook.com/thilinah)
|
||||
Copyright (c) 2018 [Glacies UG, Berlin, Germany] (http://glacies.de)
|
||||
Developer: Thilina Hasantha (http://lk.linkedin.com/in/thilinah | https://github.com/thilinah)
|
||||
*/
|
||||
/* global tinyMCE */
|
||||
const ValidationRules = {
|
||||
|
||||
ValidationRules = {
|
||||
|
||||
float: function (str) {
|
||||
var floatstr = /^[-+]?[0-9]+(\.[0-9]+)?$/;
|
||||
if (str != null && str.match(floatstr)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
number: function (str) {
|
||||
var numstr = /^[0-9]+$/;
|
||||
if (str != null && str.match(numstr)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
numberOrEmpty: function (str) {
|
||||
if(str == ""){
|
||||
return true;
|
||||
}
|
||||
var numstr = /^[0-9]+$/;
|
||||
if (str != null && str.match(numstr)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
email: function (str) {
|
||||
var emailPattern = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
|
||||
return str != null && emailPattern.test(str);
|
||||
},
|
||||
|
||||
emailOrEmpty: function (str) {
|
||||
if(str == ""){
|
||||
return true;
|
||||
}
|
||||
var emailPattern = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
|
||||
return str != null && emailPattern.test(str);
|
||||
},
|
||||
|
||||
username: function (str) {
|
||||
var username = /^[a-zA-Z0-9\.-]+$/;
|
||||
return str != null && username.test(str);
|
||||
},
|
||||
|
||||
input: function (str) {
|
||||
if (str != null && str.length > 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
float(str) {
|
||||
const floatstr = /^[-+]?[0-9]+(\.[0-9]+)?$/;
|
||||
if (str != null && str.match(floatstr)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
number(str) {
|
||||
const numstr = /^[0-9]+$/;
|
||||
if (str != null && str.match(numstr)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
numberOrEmpty(str) {
|
||||
if (str === '') {
|
||||
return true;
|
||||
}
|
||||
const numstr = /^[0-9]+$/;
|
||||
if (str != null && str.match(numstr)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
email(str) {
|
||||
const emailPattern = /^\s*[\w\-+_]+(\.[\w\-+_]+)*@[\w\-+_]+\.[\w\-+_]+(\.[\w\-+_]+)*\s*$/;
|
||||
return str != null && emailPattern.test(str);
|
||||
},
|
||||
|
||||
emailOrEmpty(str) {
|
||||
if (str === '') {
|
||||
return true;
|
||||
}
|
||||
const emailPattern = /^\s*[\w\-+_]+(\.[\w\-+_]+)*@[\w\-+_]+\.[\w\-+_]+(\.[\w\-+_]+)*\s*$/;
|
||||
return str != null && emailPattern.test(str);
|
||||
},
|
||||
|
||||
username(str) {
|
||||
const username = /^[a-zA-Z0-9.-]+$/;
|
||||
return str != null && username.test(str);
|
||||
},
|
||||
|
||||
input(str) {
|
||||
if (str != null && str.length > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
|
||||
};
|
||||
|
||||
function FormValidation(formId,validateAll,options) {
|
||||
class FormValidation {
|
||||
constructor(formId, validateAll, options) {
|
||||
this.tempOptions = {};
|
||||
this.formId = formId;
|
||||
this.formError = false;
|
||||
this.formId = formId;
|
||||
this.formError = false;
|
||||
this.formObject = null;
|
||||
this.errorMessages = "";
|
||||
this.errorMessages = '';
|
||||
this.popupDialog = null;
|
||||
this.validateAll = validateAll;
|
||||
this.errorMap = new Array();
|
||||
this.errorMap = [];
|
||||
|
||||
this.settings = {"thirdPartyPopup":null,"LabelErrorClass":false, "ShowPopup":true};
|
||||
this.settings = { thirdPartyPopup: null, LabelErrorClass: false, ShowPopup: true };
|
||||
|
||||
this.settings = jQuery.extend(this.settings,options);
|
||||
this.settings = jQuery.extend(this.settings, options);
|
||||
|
||||
this.inputTypes = new Array( "text", "radio", "checkbox", "file", "password", "select-one","select-multi", "textarea","fileupload" ,"signature");
|
||||
this.inputTypes = ['text', 'radio', 'checkbox', 'file', 'password', 'select-one', 'select-multi', 'textarea', 'fileupload', 'signature'];
|
||||
|
||||
this.validator = ValidationRules;
|
||||
}
|
||||
|
||||
}
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
clearError(formInput, overrideMessage) {
|
||||
const id = formInput.attr('id');
|
||||
$(`#${this.formId} #field_${id}`).removeClass('error');
|
||||
$(`#${this.formId} #help_${id}`).html('');
|
||||
}
|
||||
|
||||
FormValidation.method('clearError' , function(formInput, overrideMessage) {
|
||||
var id = formInput.attr("id");
|
||||
$('#'+ this.formId +' #field_'+id).removeClass('error');
|
||||
$('#'+ this.formId +' #help_'+id).html('');
|
||||
});
|
||||
|
||||
FormValidation.method('addError' , function(formInput, overrideMessage) {
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
addError(formInput, overrideMessage) {
|
||||
this.formError = true;
|
||||
if(formInput.attr("message") != null) {
|
||||
this.errorMessages += (formInput.attr("message") + "\n");
|
||||
this.errorMap[formInput.attr("name")] = formInput.attr("message");
|
||||
}else{
|
||||
this.errorMap[formInput.attr("name")] = "";
|
||||
if (formInput.attr('message') != null) {
|
||||
this.errorMessages += (`${formInput.attr('message')}\n`);
|
||||
this.errorMap[formInput.attr('name')] = formInput.attr('message');
|
||||
} else {
|
||||
this.errorMap[formInput.attr('name')] = '';
|
||||
}
|
||||
|
||||
var id = formInput.attr("id");
|
||||
var validation = formInput.attr("validation");
|
||||
var message = formInput.attr("validation");
|
||||
$('#'+ this.formId +' #field_'+id).addClass('error');
|
||||
if(message == undefined || message == null || message == ""){
|
||||
$('#'+ this.formId +' #help_err_'+id).html(message);
|
||||
}else{
|
||||
if(validation == undefined || validation == null || validation == ""){
|
||||
$('#'+ this.formId +' #help_err_'+id).html("Required");
|
||||
}else{
|
||||
if(validation == "float" || validation == "number"){
|
||||
$('#'+ this.formId +' #help_err_'+id).html("Number required");
|
||||
}else if(validation == "email"){
|
||||
$('#'+ this.formId +' #help_err_'+id).html("Email required");
|
||||
}else{
|
||||
$('#'+ this.formId +' #help_err_'+id).html("Required");
|
||||
}
|
||||
const id = formInput.attr('id');
|
||||
const validation = formInput.attr('validation');
|
||||
const message = formInput.attr('validation');
|
||||
$(`#${this.formId} #field_${id}`).addClass('error');
|
||||
if (message === undefined || message == null || message === '') {
|
||||
$(`#${this.formId} #help_err_${id}`).html(message);
|
||||
} else if (validation === undefined || validation == null || validation === '') {
|
||||
$(`#${this.formId} #help_err_${id}`).html('Required');
|
||||
} else if (validation === 'float' || validation === 'number') {
|
||||
$(`#${this.formId} #help_err_${id}`).html('Number required');
|
||||
} else if (validation === 'email') {
|
||||
$(`#${this.formId} #help_err_${id}`).html('Email required');
|
||||
} else {
|
||||
$(`#${this.formId} #help_err_${id}`).html('Required');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
showErrors() {
|
||||
if (this.formError) {
|
||||
if (this.settings.thirdPartyPopup !== undefined && this.settings.thirdPartyPopup != null) {
|
||||
this.settings.thirdPartyPopup.alert();
|
||||
} else if (this.settings.ShowPopup === true) {
|
||||
if (this.tempOptions.popupTop !== undefined && this.tempOptions.popupTop != null) {
|
||||
this.alert('Errors Found', this.errorMessages, this.tempOptions.popupTop);
|
||||
} else {
|
||||
this.alert('Errors Found', this.errorMessages, -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
FormValidation.method('showErrors' , function() {
|
||||
if(this.formError) {
|
||||
if(this.settings['thirdPartyPopup'] != undefined && this.settings['thirdPartyPopup'] != null){
|
||||
this.settings['thirdPartyPopup'].alert();
|
||||
}else{
|
||||
if(this.settings['ShowPopup'] == true){
|
||||
if(this.tempOptions['popupTop'] != undefined && this.tempOptions['popupTop'] != null){
|
||||
this.alert("Errors Found",this.errorMessages,this.tempOptions['popupTop']);
|
||||
}else{
|
||||
this.alert("Errors Found",this.errorMessages,-1);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
FormValidation.method('checkValues' , function(options) {
|
||||
checkValues(options) {
|
||||
this.tempOptions = options;
|
||||
var that = this;
|
||||
const that = this;
|
||||
this.formError = false;
|
||||
this.errorMessages = "";
|
||||
this.formObject = new Object();
|
||||
var validate = function (inputObject) {
|
||||
if(that.settings['LabelErrorClass'] != false){
|
||||
$("label[for='" + name + "']").removeClass(that.settings['LabelErrorClass']);
|
||||
}
|
||||
var id = inputObject.attr("id");
|
||||
var name = inputObject.attr("name");
|
||||
var type = inputObject.attr("type");
|
||||
this.errorMessages = '';
|
||||
this.formObject = {};
|
||||
// eslint-disable-next-line consistent-return
|
||||
const validate = function (inputObject) {
|
||||
let inputValue = null;
|
||||
const name = inputObject.attr('name');
|
||||
if (that.settings.LabelErrorClass !== false) {
|
||||
$(`label[for='${name}']`).removeClass(that.settings.LabelErrorClass);
|
||||
}
|
||||
const id = inputObject.attr('id');
|
||||
const type = inputObject.attr('type');
|
||||
|
||||
if(inputObject.hasClass('select2-focusser') || inputObject.hasClass('select2-input')){
|
||||
return true;
|
||||
if (inputObject.hasClass('select2-focusser') || inputObject.hasClass('select2-input')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (jQuery.inArray(type, that.inputTypes) >= 0) {
|
||||
if (inputObject.hasClass('uploadInput')) {
|
||||
inputValue = inputObject.attr('val');
|
||||
} else if (type === 'radio' || type === 'checkbox') {
|
||||
inputValue = $(`input[name='${name}']:checked`).val();
|
||||
} else if (inputObject.hasClass('select2Field')) {
|
||||
if ($(`#${that.formId} #${id}`).select2('data') != null && $(`#${that.formId} #${id}`).select2('data') !== undefined) {
|
||||
inputValue = $(`#${that.formId} #${id}`).select2('data').id;
|
||||
} else {
|
||||
inputValue = '';
|
||||
}
|
||||
} else if (inputObject.hasClass('select2Multi')) {
|
||||
if ($(`#${that.formId} #${id}`).select2('data') != null && $(`#${that.formId} #${id}`).select2('data') !== undefined) {
|
||||
const inputValueObjects = $(`#${that.formId} #${id}`).select2('data');
|
||||
inputValue = [];
|
||||
for (let i = 0; i < inputValueObjects.length; i++) {
|
||||
inputValue.push(inputValueObjects[i].id);
|
||||
}
|
||||
inputValue = JSON.stringify(inputValue);
|
||||
} else {
|
||||
inputValue = '';
|
||||
}
|
||||
} else if (inputObject.hasClass('signatureField')) {
|
||||
if ($(`#${that.formId} #${id}`).data('signaturePad').isEmpty()) {
|
||||
inputValue = '';
|
||||
} else {
|
||||
inputValue = $(`#${id}`).data('signaturePad').toDataURL();
|
||||
}
|
||||
} else if (inputObject.hasClass('simplemde')) {
|
||||
inputValue = $(`#${that.formId} #${id}`).data('simplemde').value();
|
||||
} else if (inputObject.hasClass('tinymce')) {
|
||||
inputValue = tinyMCE.get(id).getContent({ format: 'raw' });
|
||||
} else {
|
||||
inputValue = inputObject.val();
|
||||
}
|
||||
|
||||
if(jQuery.inArray(type, that.inputTypes ) >= 0) {
|
||||
if(inputObject.hasClass('uploadInput')){
|
||||
inputValue = inputObject.attr("val");
|
||||
//}else if(inputObject.hasClass('datetimeInput')){
|
||||
//inputValue = inputObject.getDate()+":00";
|
||||
}else{
|
||||
//inputValue = (type == "radio" || type == "checkbox")?$("input[name='" + name + "']:checked").val():inputObject.val();
|
||||
const validation = inputObject.attr('validation');
|
||||
let valid = false;
|
||||
|
||||
inputValue = null;
|
||||
if(type == "radio" || type == "checkbox"){
|
||||
inputValue = $("input[name='" + name + "']:checked").val();
|
||||
}else if(inputObject.hasClass('select2Field')){
|
||||
if($('#'+id).select2('data') != null && $('#'+id).select2('data') != undefined){
|
||||
inputValue = $('#'+id).select2('data').id;
|
||||
}else{
|
||||
inputValue = "";
|
||||
}
|
||||
|
||||
}else if(inputObject.hasClass('select2Multi')){
|
||||
if($('#'+id).select2('data') != null && $('#'+id).select2('data') != undefined){
|
||||
inputValueObjects = $('#'+id).select2('data');
|
||||
inputValue = [];
|
||||
for(var i=0;i<inputValueObjects.length;i++){
|
||||
inputValue.push(inputValueObjects[i].id);
|
||||
}
|
||||
inputValue = JSON.stringify(inputValue);
|
||||
}else{
|
||||
inputValue = "";
|
||||
}
|
||||
}else if(inputObject.hasClass('signatureField')){
|
||||
if($('#'+id).data('signaturePad').isEmpty()){
|
||||
inputValue = '';
|
||||
}else{
|
||||
inputValue = $('#'+id).data('signaturePad').toDataURL();
|
||||
}
|
||||
}else if(inputObject.hasClass('simplemde')){
|
||||
inputValue = $('#'+id).data('simplemde').value();
|
||||
|
||||
}else if(inputObject.hasClass('tinymce')){
|
||||
inputValue = tinyMCE.get(id).getContent({format : 'raw'});
|
||||
|
||||
}else{
|
||||
inputValue = inputObject.val();
|
||||
}
|
||||
}
|
||||
|
||||
var validation = inputObject.attr('validation');
|
||||
var valid = false;
|
||||
|
||||
if(validation != undefined && validation != null && that.validator[validation] != undefined && that.validator[validation] != null){
|
||||
valid = that.validator[validation](inputValue);
|
||||
|
||||
}else{
|
||||
|
||||
if(that.validateAll){
|
||||
if(validation != undefined && validation != null && validation == "none"){
|
||||
valid = true;
|
||||
}else{
|
||||
valid = that.validator['input'](inputValue);
|
||||
}
|
||||
|
||||
}else{
|
||||
valid = true;
|
||||
}
|
||||
$(that.formObject).attr(id,inputValue);
|
||||
}
|
||||
|
||||
if(!valid) {
|
||||
that.addError(inputObject, null);
|
||||
}else{
|
||||
that.clearError(inputObject, null);
|
||||
$(that.formObject).attr(id,inputValue);
|
||||
if (validation !== undefined && validation != null && that.validator[validation] !== undefined && that.validator[validation] != null) {
|
||||
valid = that.validator[validation](inputValue);
|
||||
} else {
|
||||
if (that.validateAll) {
|
||||
if (validation !== undefined && validation != null && validation === 'none') {
|
||||
valid = true;
|
||||
} else {
|
||||
valid = that.validator.input(inputValue);
|
||||
}
|
||||
} else {
|
||||
valid = true;
|
||||
}
|
||||
that.formObject[id] = inputValue;
|
||||
}
|
||||
|
||||
if (!valid) {
|
||||
that.addError(inputObject, null);
|
||||
} else {
|
||||
that.clearError(inputObject, null);
|
||||
that.formObject[id] = inputValue;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var inputs = $('#'+ this.formId + " :input");
|
||||
inputs.each(function() {
|
||||
var that = $(this);
|
||||
validate(that);
|
||||
let inputs = $(`#${this.formId} :input`);
|
||||
inputs.each(function () {
|
||||
validate($(this));
|
||||
});
|
||||
|
||||
inputs = $('#'+ this.formId + " .uploadInput");
|
||||
inputs.each(function() {
|
||||
var that = $(this);
|
||||
validate(that);
|
||||
inputs = $(`#${this.formId} .uploadInput`);
|
||||
inputs.each(function () {
|
||||
validate($(this));
|
||||
});
|
||||
|
||||
this.showErrors();
|
||||
this.tempOptions = {};
|
||||
return !this.formError;
|
||||
});
|
||||
}
|
||||
|
||||
FormValidation.method('getFormParameters' , function() {
|
||||
getFormParameters() {
|
||||
return this.formObject;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
FormValidation.method('alert', function (title,text,top) {
|
||||
alert(title, text) {
|
||||
alert(text);
|
||||
});
|
||||
}
|
||||
|
||||
static getValidationRules() {
|
||||
return ValidationRules;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default FormValidation;
|
||||
|
||||
Reference in New Issue
Block a user