Wednesday, 28 August 2013

Try to set custom validation message with jQuery Remote

Try to set custom validation message with jQuery Remote

I have the a form with the following validation setup:
$('#username').rules("add", {
onfocusout: false,
onkeyup: false,
required: true,
email: true,
remote: {
url: function,
type: 'GET',
dataType: 'json',
traditional: true,
data: {
username: function () {
return $('#username').val();
}
},
dataFilter: function (responseString) {
var response = jQuery.parseJSON(responseString);
currentMessage = response.Message;
if (response.State == 0) {
currentMessage = currentMessage + 0;
return false;
}
return 'true';
},
beforeSend: function (response) {
showLoadingIndicator($('#username'));
},
complete: function () {
hideLoadingIndicator($('#username'));
}
},
messages: {
remote: function () { return currentMessage; }
},
success: function (label) {
$('[data-valmsg-for="Username"]span').html(currentMessage);
}
});
What this is attempting to do is use the same validation elements (in
order to work with some other framework) to show both errors and success
methods.
My problem is that the success method of my rule gets fired BEFORE the
remote validation has completed. I tried setting the message several ways
but the custom messages parameter doesn't seem to be called on validation
success.
Does anyone know of other methods to use the validation error field for
both success and error messages when using a mix of both remote and
pattern validation rules?

No comments:

Post a Comment