jQuery验证器addmethod自定义消息 [英] jquery validator addmethod custom message
问题描述
我为jquery的验证器插件创建了一个方法,该方法的工作原理类似于远程规则.区别在于,我想显示动态错误消息(基于ajax响应).
I've created a method for jquery's validator plugin, that works like the remote rule. The difference is that I'd like to display a dynamic error message (based on the ajax response).
jQuery.validator.addMethod("duplicate", function(value, element, params) {
var object_settings = this.settings;
params.data[$(element).attr("name")] = value;
$.post(params.url, params.data, function(response) {
if (response == 'true'){ return true; }
else {
object_settings.messages[element.name] = response;
return false;
}
}, 'text');
}, '');
它起作用...它设置消息,但最初不显示它(如果您再次验证该字段,则显示消息).
It works...sort of....it sets the message, but doesn't display it initially (if you validate the field a second time, the message is displayed).
有什么建议吗?
(也许远程规则提供了此功能...我在文档中找不到任何内容)
(maybe the remote rule offers this functionality...I couldn't find anything in the documentation)
推荐答案
这是解决方案....需要调用对象的showErrors函数:
Here's the solution....needed to call the showErrors function of the object:
jQuery.validator.addMethod("duplicate", function(value, element, params) {
var validator = this;
params.data[element.name] = value;
$.post(params.url, params.data, function(response) {
if (response == 'true'){ return true; }
else {
var errors = {};
errors[element.name] = response;
validator.showErrors(errors);
return false;
}
}, 'text');
}, '');
取自jquery.validate.js中的远程"(第917-919行)
Taken from "remote" in jquery.validate.js (lines 917 - 919)
这篇关于jQuery验证器addmethod自定义消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!