liveBook · Manning

Data-val-* attribute

As explained in Pro ASP.NET Core 7, Tenth Edition

A data-val attribute is an HTML data attribute emitted by ASP.NET Core to provide client-side validation metadata.

ASP.NET Core form tag helpers emit `data-val*` attributes on input elements to describe validation constraints. `data-val="true"` marks an element for client-side validation; an attribute such as `data-val-required` supplies a rule and its error message. section 29.6 section 27.4.1

A generated ProductId input

In `Views/Form/Form.cshtml`, `<input asp-for="ProductId" />` generates a numeric input for a C# `long` property. Its rendered attributes include `data-val="true"` and `data-val-required="The ProductId field is required."` The `type="number"` hint is not a substitute for model validation: browsers differ in how they enforce input types. section 27.4.1

...
<div class="form-group">
    <label>Id</label>
    <input class="form-control" type="number" data-val="true" 
        data-val-required="The ProductId field is required." 
        id="ProductId" name="ProductId" value="1">
</div>
...

Inspect the rendered input to see the validation metadata alongside its generated `type`, `id`, and `name` attributes. section 27.4.1

To use the metadata in the browser, install jQuery Validate and jQuery Validation Unobtrusive, then load jQuery, jQuery Validate, and the unobtrusive script in that order. The unobtrusive code finds `data-val` elements when the form is submitted. If validation fails, it displays an error and stops submission before an HTTP request is sent. Check the browser’s F12 tools while submitting an invalid form to verify that no request occurs. section 29.6

Listing 29.24 Adding elements in the _Validation.cshtml file in the Views/Shared folder
<script type="text/javascript">
    window.addEventListener("DOMContentLoaded", () => {
        document.querySelectorAll("input.input-validation-error")
            .forEach((elem) => { elem.classList.add("is-invalid"); }
        );
    });
</script>
<script src="/lib/jquery/jquery.min.js"></script>
<script src="/lib/jquery-validate/jquery.validate.min.js"></script>
<script src=
  "/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js">
</script>

Use the `_Validation.cshtml` listing for the script paths and required load order. section 29.6

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Shows generated attributes and the scripts that consume them, but does not detail the JavaScript validator’s internals.
Examples
Uses generated ProductId and Name inputs, validation script listings, and an F12 check for absent HTTP requests.
What it assumes
Assumes an ASP.NET Core form-tag-helper project and provides the package installation and script setup needed for client-side validation.
What you can do after
Apply it via jQuery Validation and jQuery Validation Unobtrusive to display form errors without submitting invalid data.
How it connects
Connects form tag helpers and model validation with jQuery Validation Unobtrusive, browser validation, and HTTP form submission.

Also explained in

Compare how 2 books explain data-val-* attribute →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.