I have an ASP.NET MVC 5 base project in which I use css bootstrap for designing my pages.
To work property with css bootstrap. I want to be able to override the default behavior of the errorElement
, errorClass
, highlight
,unhighlight
and errorPlacement
properties/functions.
This is what I have tried
First, I included the packages in the following order
- jQuery (v3.1.1)
- jquery.validate.min.js (v1.15.0)
- My settings where I override the default behavior (Code below)
- jquery.validate.unobtrusive.js (v3.2.3)
This is my first attempt into overriding the package
$.validator.setDefaults({
errorElement: "span",
errorClass: "help-block",
highlight: function (element, errorClass, validClass) {
$(element).closest('.form-group').addClass('has-error');
},
unhighlight: function (element, errorClass, validClass) {
$(element).closest('.form-group').removeClass('has-error');
},
errorPlacement: function (error, element) {
var elm = $(element);
console.log('errorPlacement was called');
if (elm.parent('.input-group').length) {
console.log('parent');
console.log(elm.parent());
error.insertAfter(elm.parent());
}
else if (elm.prop('type') === 'checkbox' || elm.prop('type') === 'radio') {
error.appendTo(elm.closest(':not(input, label, .checkbox, .radio)').first());
} else {
error.insertAfter(elm);
}
}
});
However, the above code will never call the errorPlacement
function, other than that everything else works as expected. But since the errorPlacement
method is not being called, the error is being placed in a different place.
Then I changed the above code to this (overriding both jQuery validation and the Unobtrusive)
$.validator.setDefaults({
errorElement: "span",
errorClass: "help-block",
highlight: function (element, errorClass, validClass) {
console.log('highlight was called');
$(element).closest('.form-group').addClass('has-error');
},
unhighlight: function (element, errorClass, validClass) {
console.log('highlight was called');
$(element).closest('.form-group').removeClass('has-error');
},
errorPlacement: function (error, element) { }
});
$.validator.unobtrusive.options = {
errorPlacement: function (error, element) {
var elm = $(element);
console.log('errorPlacement was called');
if (elm.parent('.input-group').length || elm.parent('.input-group-custom').length) {
console.log('parent');
console.log(elm.parent());
error.insertAfter(elm.parent());
}
else if (elm.prop('type') === 'checkbox' || elm.prop('type') === 'radio') {
error.appendTo(elm.closest(':not(input, label, .checkbox, .radio)').first());
} else {
error.insertAfter(elm);
}
}
};
Now, the function errorPlacement
is being called. But, the error is being inserted multiple time. Somehow the code the removes the error message is not being called now.
How can I fix this issue?
$.validator.setDefaults()
inside or outside of your document ready function? – Bearskin$(function () {....});
block. To make sure I am clear on what did not work is that the functionerrorPlacement
was not called. but the rest worked fine. – Disparate.setDefaults()
is being called after the.validate()
initialization. The only way to force.setDefaults()
to get called before.validate()
in this case is to place it outside of your document ready. – Bearskin.setDefaults()
is being ignored. – Bearskin.setDefaults()
to get called before.validate()
in this case (with unobtrusive) is to place it (setDefaults) outside of your document ready." – Bearskin