如何将自定义客户端验证添加到从 MVC5 VB.NET 中的注释生成的验证中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32595090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add custom client side validation to validation that was generated from annotations in MVC5 VB.NET
提问by hobwell
I have added a number of annotations to do simple required field validation in a modal form. Now I need to do a somewhat convoluted check for uniqueness on a couple of the fields involved.
我添加了许多注释以在模态表单中进行简单的必填字段验证。现在我需要对所涉及的几个字段的唯一性进行一些复杂的检查。
I want to avoid using a custom annotation to do thisas the fields that require validation within the model require extra information that does not exist in the model.
我想避免使用自定义注释来执行此操作,因为模型中需要验证的字段需要模型中不存在的额外信息。
I want to be able to add a new rule to the existing validation via script.
我希望能够通过脚本向现有验证添加新规则。
Unfortunately I can't seem to get both the new rule and the existing ones to work at the same time.
不幸的是,我似乎无法同时使用新规则和现有规则。
If I do the following, then only the new rulegets applied, all the existing rules (required fields etc.) disappear.
如果我执行以下操作,则只会应用新规则,所有现有规则(必填字段等)都会消失。
jQuery.validator.addMethod("uniqueresourceid",
function(value, element, options) {
return ResourceIDValidation(options.resourceSet, value, options.originalresourceidpropertyname);
}, "This resource ID is already inuse.");
var validator = $('#InstitutionModalForm').validate();
$('#Institution_NameResourceID').rules("add", {
uniqueresourceid: {
resourceSet: "Institutions",
resourceId: $('#NameResourceID').val(),
oldResourceId: "OriginalNameResourceID"
}
});
function ResourceIDValidation(ResourceSet, ResourceID, OldResourceIDField) {
var valid = false;
$.ajax({
type: "POST",
url: "@Url.Action("ValidateResourceID", "Admin")",
traditional: true,
contentType: 'application/json; charset=utf-8',
async: false,
data: JSON.stringify({
ResourceSet: ResourceSet,
ResourceID: ResourceID,
OldResourceID: $('#' + OldResourceIDField).val(),
}),
success: function (result) {
valid = result;
},
error: function (result) {
console.log(data);
valid = false;
}
});
return valid;
}
If I remove the 'var validator =...' line, then only the originalvalidation (required fields etc.) fires.
如果我删除 'var validator =...' 行,则只会触发原始验证(必填字段等)。
I can't see any reason why this shouldn't be possible, but I can't seem to figure out how to make it work.
我看不出有任何理由为什么这不应该是可能的,但我似乎无法弄清楚如何让它发挥作用。
I'm not really clear on how the unobtrusive stuff does it's magic, but shouldn't there be a way to hook into whatever validator is being generated by the server side annotations so that I can add a new rule in the JS?
我不太清楚不显眼的东西是如何神奇的,但不应该有一种方法可以连接到服务器端注释生成的任何验证器,以便我可以在 JS 中添加新规则吗?
回答by hobwell
Strictly speaking, this is possiblebut I can see some very important reasons why you wouldn't want to do this.
严格来说,这是可能的,但我可以看到您不想这样做的一些非常重要的原因。
Most importantly, this creates a client side validation only. This means if someone nefariously submits the form, or if they don't have JS enabled, that your server side code will not do this validation and probably break.
最重要的是,这只会创建客户端验证。这意味着如果有人恶意提交表单,或者如果他们没有启用 JS,则您的服务器端代码将不会执行此验证并且可能会中断.
In addition to that, it makes your code harder to maintain as the validation is not handily visible in your code as it would be with an annotation.
除此之外,它使您的代码更难维护,因为验证在您的代码中不像使用注释那样方便可见。
While it is probably a very bad idea to do this, this is how you could make it happen. Adding this code above the "addMethod" caused it to validate as I initially intended.
虽然这样做可能是一个非常糟糕的主意,但您可以这样做。在“addMethod”上方添加此代码使其按照我最初的预期进行验证。
$('#NameResourceID').attr("data-val", "true");
$('#NameResourceID').attr("data-val-uniqueresourceid", "Resource ID must be unique.");
$('#NameResourceID').attr("data-val-uniqueresourceid-resourceset","institutions");
$('#NameResourceID').attr("data-val-uniqueresourceid-originalresourceidpropertyname","OriginalNameResourceID");
Do this instead: Implement a custom validation annotation as Stephen Muecke suggested in the question comments. It was more work, but in the end is far better practice. Thiswas the most helpful (to me) tutorial I could find on the subject.
请改为执行此操作:按照 Stephen Muecke 在问题评论中的建议实施自定义验证注释。这是更多的工作,但最终是更好的实践。 这是我能找到的关于这个主题的最有用的(对我来说)教程。

