是否可以使用 MVC 非侵入式适配器在验证功能期间自定义 jquery 验证消息?
我有一个 MVC3 表单中的多个select
列表框。用户可以添加&从该列表框中删除自定义 option
元素。但是我想验证列表框中没有两个选项包含相同的值。
我创建了一个实现 IClientValidatable 的自定义 ValidationAttribute。服务器和客户端验证都正常工作。但是,我的 ErrorMessage 看起来像这样:
ErrorMessage = "{0} has more than 1 '{1}' option.")
不幸的是,我只能将部分格式化的消息传递给客户端验证消息,并且我这样做:
var rule = new ModelClientValidationRule
{
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
ValidationType = "noduplicatestringvalues",
};
在运行验证之前,验证不会知道哪个选项值是重复的,所以我想在验证功能期间完成消息格式化。
但是,似乎只能在 $.validator.unobtrusive.adapters.add()
函数期间设置错误消息。
我尝试在验证期间在 select
元素上设置 data-val-noduplicatestringvalues
属性,但错误消息仍然显示如下:
FieldName has more than 1 '{ 1}' 项。
有什么方法可以更改验证功能期间的错误消息吗?
I have a multiple select
listbox in an MVC3 form. A user can add & remove custom option
elements to and from this listbox. However I want to validate that no 2 options in the listbox contain the same value.
I have created a custom ValidationAttribute that implements IClientValidatable. Both the server and the client validation are working. However, my ErrorMessage looks like this:
ErrorMessage = "{0} has more than 1 '{1}' option.")
Unfortunately I can only pass a partially formatted message to the client validation message, and I do it like so:
var rule = new ModelClientValidationRule
{
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
ValidationType = "noduplicatestringvalues",
};
The validation won't know which option value is duplicated until the validation is run, so I want to complete the message formatting during the validation function.
However, it seems that the error message can only be set during the $.validator.unobtrusive.adapters.add()
function.
I have tried setting the data-val-noduplicatestringvalues
attribute on the select
element during the validation, but the error message is still displayed like so:
FieldName has more than 1 '{1}' item.
Is there any way to change the error message during the validation function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以自定义消息。
如果您查看
jquery.validate.js
中的remote
验证方法,您将看到一个自定义错误消息的示例,在这种情况下使用从远程返回的消息验证功能。You can customize the message.
If you look at the
remote
validation method injquery.validate.js
, you will see an example of customizing the error message, in that case using a message returned from a remote validation function.