gpt4 book ai didi

jquery - 了解 $.validator.unobtrusive.adapters.addBool() 方法

转载 作者:行者123 更新时间:2023-12-03 21:58:51 26 4
gpt4 key购买 nike

我正在尝试理解一些事情。

摘自这篇博文http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html

Bridging HTML and jQuery Validate: Adapters

Writing a client-side validator involves two steps: writing the validator for jQuery Validate, and writing the adapter which takes the parameter values from the HTML attributes and turns it into jQuery Validate metadata. The former topic is not in the scope of this blog post (since it’s really not MVC specific).

There is an adapter collection available at jQuery.validator.unobtrusive.adapters. Hanging off the adapter collection is the adapter registration method (add) and three helpers that can be used to register very common types of adapters (addBool, addSingleVal, and addMinMax).

请注意,它显示步。

但是如果你看一下这篇文章MVC3: make checkbox required via jQuery validate?您只需要第二步(“编写适配器”)即可进行验证 - 通过添加以下代码行:

$.validator.unobtrusive.adapters.addBool("mandatory", "required");

我在新的 MVC 4 Internet 应用程序中测试了代码,它工作正常,这是 super 简单的示例。

查看模型

public class SimpleViewModel
{
[Mandatory(ErrorMessage = "You must agree to the Terms to register.")]
[Display(Name = "Terms Accepted")]
public bool IsTermsAccepted { get; set; }
}

验证属性

public class MandatoryAttribute : ValidationAttribute, IClientValidatable
{
public override bool IsValid(object value)
{
return (!(value is bool) || (bool)value);
}

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientValidationRule rule = new ModelClientValidationRule();
rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
rule.ValidationType = "mandatory";
yield return rule;
}
}

查看

@model MvcApplication2.Models.SimpleViewModel

@{
ViewBag.Title = "";
}

@using (Html.BeginForm()) {
@Html.ValidationSummary()
@Html.CheckBoxFor(model => model.IsTermsAccepted)
@Html.ValidationMessageFor(model => model.IsTermsAccepted)
<input type="submit" value="Send" />
}

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$.validator.unobtrusive.adapters.addBool("mandatory", "required");
</script>
}

所以基本上我有三个问题:

  1. 是 $.validator.unobtrusive.adapters.addBool("mandatory", "required");除了编写属性类之外,真的是您唯一需要的吗?

  2. 它在幕后到底做了什么?

  3. 在哪里可以找到有关 addBool 的优秀文档?

最佳答案

除了评论中链接到的文章@BlueChippy之外,我在this article中找到了2.的答案。 .

  1. 是的,这是除了属性之外唯一需要的东西。这是因为我们使用了已经存在的规则(必需)。
  2. 它有什么作用?

This just registers a new validation adapter for the MandatoryAttribute, where the first parameter is the adapter name and the second parameter is the name of jQuery validate rule. The adapter name should match the value we specified earlier as the validation type, and the jQuery validation required-rule will require the user to check the checkbox.

3.更多信息可参见this article on Brad Wilson's blog .

关于jquery - 了解 $.validator.unobtrusive.adapters.addBool() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11978990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com