Understanding the $.validator.unobtrusive.adapters.addBool() method

19,505

Besides the article @BlueChippy linked to in the comments, I found the answer to 2. in this article.

  1. yes that's the only thing needed besides the attribute. That is because we use a rule that already exists (required).
  2. What does it do?

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 . More information can be found in this article on Brad Wilson's blog.

Share:
19,505
PussInBoots
Author by

PussInBoots

Fullstack. Anything web. But mostly .NET, MVC, Razor Pages, C#, HTML, CSS, JavaScript, Docker, Kubernetes, Azure etc.

Updated on June 05, 2022

Comments

  • PussInBoots
    PussInBoots over 1 year

    I'm trying to understand something.

    From this blogpost 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).

    Notice that it says two steps.

    But if you take a look at this post MVC3: make checkbox required via jQuery validate? you only need the second step ("writing the adapter") for the validation to work - by adding this line of code:

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

    I tested out the code in a new MVC 4 Internet App and it works fine, here's the supersimple sample.

    View Model

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

    Validation Attribute

    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;
        }
    }
    

    View

    @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>
    }
    

    So basically I've got three questions:

    1. is $.validator.unobtrusive.adapters.addBool("mandatory", "required"); really the only thing you need besides writing an attribute class?

    2. what exactly does it do behind the scenes?

    3. where can I find good documentation about addBool?