What is the proper way to override the ErrorPlacement function in jquery.validate.unobtrusive?
Looking at the jquery.validate.unobtrusive.js script it appears that the developers' intention would allow you to apply your own ErrorPlacement function by setting $jQval.unobtrusive.options.
In the validationInfo(form) function that defines the errorPlacement function, we see a call to execInContext("errorPlacement", arguments).
It would seem that if we create an errorPlacement function under $.validator.unobtrusive.options, then this will be called.
$.validator.unobtrusive.options = {
errorPlacement: function () {
console.log("hello");
}
};
The issue is that this must configured after jquery.validate.js and before jquery.validate.unobtrusive.js is referenced. Otherwise $jQval.unobtrusive.options is null and $form.data(data_validation, result) will not be set again.
function validationInfo(form) {
var $form = $(form),
result = $form.data(data_validation),
onResetProxy = $.proxy(onReset, form),
defaultOptions = $jQval.unobtrusive.options || {},
execInContext = function (name, args) {
var func = defaultOptions[name];
func && $.isFunction(func) && func.apply(form, args);
}
if (!result) {
result = {
options: { // options structure passed to jQuery Validate's validate() method
errorClass: defaultOptions.errorClass || "input-validation-error",
errorElement: defaultOptions.errorElement || "span",
errorPlacement: function () {
onError.apply(form, arguments);
execInContext("errorPlacement", arguments);
},
invalidHandler: function () {
onErrors.apply(form, arguments);
execInContext("invalidHandler", arguments);
},
messages: {},
rules: {},
success: function () {
onSuccess.apply(form, arguments);
execInContext("success", arguments);
}
},
attachValidation: function () {
$form
.off("reset." + data_validation, onResetProxy)
.on("reset." + data_validation, onResetProxy)
.validate(this.options);
},
validate: function () { // a validation function that is called by unobtrusive Ajax
$form.validate();
return $form.valid();
}
};
$form.data(data_validation, result);
}
return result;
}
The other way to get deal with this is to:
set the
$.validator.unobtrusive.optionserrorPlacementfunctionremove the unobtrusive validation
re-apply the unobtrusive validation
$.validator.unobtrusive.options = {
errorPlacement: function () {
console.log("errorPlacement");
}
};
$("#form_selector").removeData("unobtrusiveValidation");
// reapply the form's validator
$.validator.unobtrusive.parse(document);
Or the other option would be to override the function call.
var validator = $("#form_selector").data('validator');
var originalFunction = validator.settings.errorPlacement;
validator.settings.errorPlacement = function(error,element) {
console.log("errorPlacement");
originalFunction(error, element);
};
Which is the proper way of implementing your own errorPlacement method?