jquery-validation-unobtrusive: Unable to override errorPlacement function using $.validator.setDefaults()
I am trying to change the error placement to fit css-bootstrap3
I added the following code before I included jquery.validate.unobtrusive v3.2.9
But my errorPlacement
function never gets called. also adding errorClass: "help-block"
does not override the jquery.validate.unobtrusive
default class-name.
$(function () {
$.validator.setDefaults({
errorElement: "span",
errorClass: "help-block",
highlight: function (element, errorClass, validClass) {
console.log('Also, the param errorClass returns input-validation-error instead of help-block', errorClass, validClass, element);
var elm = $(element);
var group = elm.closest('.form-group');
if(group.length == 0) {
group = elm.closest('.form-group-custom');
}
if(group.length) {
group.addClass('has-error');
}
},
unhighlight: function (element, errorClass, validClass) {
console.log('Also, the param errorClass returns input-validation-error instead of help-block', errorClass, validClass, element);
var elm = $(element);
var group = elm.closest('.form-group');
if(group.length == 0) {
group = elm.closest('.form-group-custom');
}
if(group.length) {
group.removeClass('has-error');
}
},
errorPlacement: function (error, element) {
console.log('errorPlacement... this never gets called :(', error, element);
var elm = $(element);
if (elm.parent('.input-group').length || elm.parent('.input-group-custom').length) {
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);
}
},
submitHandler: function (form) {
showLoading();
form.submit();
}
});
});
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 17 (8 by maintainers)
Any reason why $.validator.defaults doesn’t get passed on to $.validator.unobtrusive.options by default? That would surely simplify things a bit…
Agreed with @Nettsentrisk. I’ve been using ASP.NET Core with jQuery Unobtrusive Validation for about a year now, and trying to modify the default validation behavior has always been a nightmare since we have no control over when
$.validate()
is called and$.validator.setDefaults()
seems to only work sporadically. There at least needs to be some official info on the Model Validation topic about how to change the jQuery Validate settings correctly within ASP.NET Core.