Skip to content

Commit 00e555b

Browse files
- Issue on element error response overriding [FIXED].
- Improved log management.
1 parent b9c73ca commit 00e555b

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

src/demo/index3.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
forceFilter: true,
112112
onChange: true,
113113
errorClass: '__error_cap',
114+
// log:true,
114115
message: {
115116
required: 'This field is required.',
116117
min: 'This field length is too low.',

src/js/formValidator.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ var jsValidator = {
5252
* Initiating the Validator.
5353
*/
5454
init: function (option) {
55-
jsLogger.table(option);
5655
// To Update global options.
5756
this.option = option;
57+
jsLogger.table(option);
5858
// Updating the filter flag to global.
5959
this.onlyFilter = option.onlyFilter;
6060
// To Enable/Disable global validator.
@@ -606,7 +606,8 @@ var jsRuleSets = {
606606
if (activeElem.name !== '') {
607607
var elem = document.getElementById(activeElem.name + '_new1_1_1xv_resp');
608608
if (typeof (elem) !== 'undefined' && elem !== null) {
609-
elem.innerHTML = '';
609+
// Remove element to avoid un-necessary buffer.
610+
elem.remove();
610611
}
611612
}
612613
}
@@ -769,10 +770,15 @@ var jsFormError = {
769770
* For manage overall logging with validator.
770771
*/
771772
var jsLogger = {
773+
status: function () {
774+
return jsValidator.option.log
775+
},
772776
/*
773777
* Simple log with "heading" and "message".
774778
*/
775779
out: function (heading, message) {
780+
781+
if (true !== this.status()) return false;
776782
console.log('======' + heading + '======');
777783
console.log(message);
778784
console.log('------------------------');
@@ -781,12 +787,14 @@ var jsLogger = {
781787
* For bulk data logging.
782788
*/
783789
bulk: function (data) {
790+
if (true !== this.status()) return false;
784791
console.log(data);
785792
},
786793
/*
787794
* For log data with table.
788795
*/
789796
table: function (data) {
797+
if (true !== this.status()) return false;
790798
console.table(data);
791799
}
792800
};
@@ -818,12 +826,19 @@ var helper = {
818826
* To Scroll Up / Down to notify the item that have validation message.
819827
*/
820828
scrollToError: function () {
829+
var dummy_id = '__header_error_target_temp';
821830
var active_class = validationResponse.getClass();
822831
if (0 === document.getElementsByClassName(active_class).length) return false;
823-
document.getElementsByClassName(active_class)[0].setAttribute('id', '__header_error_target_temp');
832+
// Getting current ID of the element.
833+
var active_id = document.getElementsByClassName(active_class)[0].id;
834+
// Update first element with dummy indec ID.
835+
document.getElementsByClassName(active_class)[0].setAttribute('id', dummy_id);
836+
// Forming ID.
824837
var id = '#' + document.getElementsByClassName(active_class)[0].id;
838+
// Navigate to ID.
825839
window.location.href = id;
826-
document.getElementsByClassName(active_class)[0].removeAttribute('id');
840+
// Restore with actual ID.
841+
document.getElementsByClassName(active_class)[0].setAttribute('id', active_id);
827842
// Remove the navigated value.
828843
this.removeHash(id);
829844
},
@@ -913,12 +928,14 @@ var validationResponse = {
913928
* To handle the "input" element.
914929
*/
915930
input: function (elem) {
931+
// Initiate process for Input.
916932
this.process(elem);
917933
},
918934
/*
919935
* To handle the "select" element.
920936
*/
921937
select: function (elem) {
938+
// Initiate process for Select.
922939
this.process(elem);
923940
},
924941
getClass: function () {
@@ -928,13 +945,16 @@ var validationResponse = {
928945
* To handle the "textArea" element.
929946
*/
930947
textArea: function (elem) {
948+
// Initiate process for TextArea.
931949
this.process(elem);
932950
},
933951
/*
934952
* To process all handlers.
935953
*/
936954
process: function (elem) {
955+
// Process with initial response.
937956
var elementDefaultResponse = '';
957+
// Get active class for error response element
938958
var active_class = this.getClass();
939959
for (var i in elem) {
940960
// jsLogger.out('Element', document.getElementById(elem[i].id));
@@ -956,10 +976,8 @@ var validationResponse = {
956976
} else {
957977
// Re-use Existing response Message SPAN.
958978
spanTag.innerHTML = elementDefaultResponse;
959-
// jsLogger.out('Element Found', true);
960-
} // jsLogger.out('Error Elem', activeElem.el);
979+
}
961980
// Append HTML response to the Element.
962-
963981
activeElem.el.parentNode.insertBefore(spanTag, activeElem.el.nextSibling);
964982
}
965983
}
@@ -971,12 +989,15 @@ var validationResponse = {
971989
//jsLogger.out('error Type 0', errorType);
972990
var errorIndex = '';
973991
var activeError = '';
992+
// Getting error response message from elemnet.
974993
var elementDefaultResponse = activeElem.el.getAttribute('data-message');
975994
if (typeof elementDefaultResponse === 'undefined' || elementDefaultResponse === '' || elementDefaultResponse === null) {
976995
// Sanity check with error message object.
977996
if (typeof this.errorMessage !== 'undefined' && typeof this.errorMessage[errorType] !== 'undefined') {
997+
// Getting error type. [ex. Required, Min, Max...]
978998
errorType = this.errorMessage[errorType];
979-
activeElem.el.getAttribute('data-message');
999+
1000+
// activeElem.el.getAttribute('data-message');
9801001
if (errorType) {
9811002
//jsLogger.out('errorType', errorType);
9821003
activeError = errorType;

0 commit comments

Comments
 (0)