I have a form spread over two tabs, I would like to color the tab header red, if the containing tab contains any fields with an error.
I got it pretty much working in this --> fiddle <--. Put a value into the field and you will see how the red tab header disappears. But if you delete your entry so that the error validation kicks in right away, the tab doesn't return to red color.
By the look of it the function invalid-form.validate
doesn't hit when an error validation throws for a second time, unless I am hooking into the wrong event.
I am literally running out of events I could hook it into, any idea where to put it? :)
function mark_tab_header_error(form, element, is_error){
var id = $(form).find(element).closest('.tab-pane').attr('id');
$(form).find(element).closest('.tabbable').find('.tab_header').each(function(index, value){
if($(this).attr('href') == '#' + id){
if(is_error)
$(this).css({color: 'red'});
else
$(this).removeAttr('style');
}
});
}
$(document).ready(function () {
var validator = validation_rules('#myform');
validator.form();
function validation_rules(form) {
$.validator.addClassRules("fillone", {
require_from_group: [1, ".fillone"]
});
var validator = $(form).bind("invalid-form.validate",
function() {
if(validator.numberOfInvalids() > 0)
mark_tab_header_error(form, validator.lastElement, true);
}).validate({
errorPlacement: function (error, element) {
var field_error = $(form).find('#id_' + element.attr('name')).siblings('.field_error');
if (field_error.length > 0) {
error.appendTo(field_error);
}
$(field_error).show();
},
invalidHandler: function () {
$("#validation_summary").text(validator.numberOfInvalids() + " field(s) are invalid");
},
errorContainer: "#validation_summary",
success: function(){
mark_tab_header_error(form, validator.lastElement, false);
}
});
return validator;
}
});