I looking for a way to call an object method from the highlight
and unhighlight
methods. What I am trying to do is display a validation passed or failed icon when a section of a form passes validation.
The sample code I below I beieve represents gaps in my understanding of this
and context. I think that I may need to somehow pass in the object to the highlight
method so that setValidationIcon
is available to highlight
.
This issue suggests that possibly I could override highlight
and unhighlight
, but I am not sure if that is what I need.
Override jQuery validation highlight / unhighlight methods
The goal here is to run the setValidationIcon
method from within highlight
and unhighlight
. Existing code does not work because the method do not exist within the context of or within highlight/unhighlight
Here is the sample code. Overall, I know this code works as I had it all as functions on the global object that were being called on change
of an input. While the code works outside of the validate object, I believe it is more appropriate for it be within the object so that it simply get called as part of validation
$("#register-form").validate({
highlight: function(element, errorClass) {
this.setValidationIcon(this);
},
unhighlight: function(element, errorClass) {
this.setValidationIcon(this);
},
setValidationIcon: function(that) {
var li = that.closest("li");
var formSectionId = "#" + that.closest(".form-section").id;
if(allValid(formSectionId)) {
this.showPassed($(li));
} else {
this.showWarning($(li));
}
},
allValid: function(formSectionId) {
var required = $(formSectionId).find("input[required]");
var valid = true;
for(var i = 0; i < required.length; i++) {
element = $(required[i]);
if(!element.valid()) valid = false;
}
return valid;
},
showPassed: function(element) {
element.children("#passed").removeClass("hide");
element.children("#empty").addClass("hide");
element.children("#error").addClass("hide");
},
showWarning: function(element) {
element.children("#passed").addClass("hide");
element.children("#error").removeClass("hide");
element.children("#empty").addClass("hide");
},
});