jQuery.validate 每个控件都有自定义错误
我目前正在使用 jQuery.validate 作为验证插件...我的问题出现了,我想根据每个控件的验证类型使用自定义错误消息。
我为此使用元数据扩展,并且我考虑过只使用一个传入的通用格式化程序..因此错误消息实际上是“{0}”,当我指定我的验证器时,我可以内联这样做...
<input ... data-meta='{
validate: {
date: [ "real error message here" ]
}
}' />
将 myDate 定义为...
// override default date...
$.validator.addMethod("date", function(value, element) {
//use Date.js's parse instead of default's new Date() matching.
return this.optional(element) || !!Date.parse(value);
}, "{0}");
我不禁觉得这样做有点“肮脏”...有人有更好的解决方案吗?
I'm currently using jQuery.validate as a plugin for validation... my problem comes into play where I want to use custom error messages based on the validation type per control.
I'm using the metadata extension for this, and I've thought about just having a generic formatter that gets passed in.. so the error message is literally "{0}" and when I specify my validators, I can do so inline...
<input ... data-meta='{
validate: {
date: [ "real error message here" ]
}
}' />
With myDate defined as...
// override default date...
$.validator.addMethod("date", function(value, element) {
//use Date.js's parse instead of default's new Date() matching.
return this.optional(element) || !!Date.parse(value);
}, "{0}");
I can't help but to feel a little "dirty" doing this though... does anyone have a better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用于验证的元数据版本已经内置了对消息的支持,例如:
您可以在此处的 jQuery 验证元数据演示中看到它的实际效果。
The metadata version for validation already has built-in support for messages, for example:
You can see it in action in the jQuery validation metadata demo here.