这真的是不显眼的 jquery 验证吗?

发布于 2024-12-10 10:29:48 字数 3090 浏览 2 评论 0 原文

根据此处的定义:

Unobtrusive JavaScript 避免将内联 JavaScript 注入 HTML。这使得您的 HTML 更小、更简洁,并且可以更轻松地替换或自定义 JavaScript 库。

但是,当阅读此博客: 博主说: 现在,为了急切地执行验证,即每次用户字段聚焦时执行验证,您需要在页面底部添加此脚本。

我不知道我是否感到困惑,但对我来说,简单的 JavaScript 验证意味着页面上没有 JavaScript 代码,只有对具有逻辑的 .js 文件的引用。通过这种方式,您可以将验证逻辑与表示分离。

话虽如此,我想在下面的表单中启用不显眼的验证。我看到的唯一区别是: 1. 第 1 行:我没有。

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<EagerlyPerformingValidation.Models.UserInformation>
  1. 我应该将 jquery 逻辑添加到外部 javascript 文件并引用它。

我在这里错过了什么吗?

public class Position{

            [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]   
            public int PositionID { get; set; }

            [Required(ErrorMessage = "Position name is required.")]
            [StringLength(20, MinimumLength = 3, ErrorMessage = "Name should not be longer than 20 characters.")]
            [Display(Name = "Position name")]              
            public string name { get; set; }

            [Required(ErrorMessage = "Number of years is required")] 
            [Display(Name = "Number of years")]
            [YearsValidationAttribute(5, ErrorMessage = "{0} value must be greater than {1} years.")]        
            public int yearsExperienceRequired { get; set; }

            public virtual ICollection<ApplicantPosition> applicantPosition { get; set; }
        }




@model Data.Model.Position

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Position</legend>

            <div class="editor-label">
                @Html.LabelFor(model => model.name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.name)
                @Html.ValidationMessageFor(model => model.name)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.yearsExperienceRequired)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.yearsExperienceRequired)
                @Html.ValidationMessageFor(model => model.yearsExperienceRequired)
            </div>

            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>

According to this definition here:

Unobtrusive JavaScript avoids injecting inline JavaScript into HTML. This makes your HTML smaller and less cluttered, and makes it easier to swap out or customize JavaScript libraries.

However when reading this blog:
The blogger says:
Now for performing validation eagerly, i.e, performing validation each time when users fields focus out, you need to add this script at bottom of the page.

I dont know if I am confused, but for me Unobstrusive javascript validation means in simple words, NO JAVASCRIPT code on the page, only the references to the .js files that have the logic. In that way you separate the validation logic from the presentation.

With this said, I would like to enable unobstrusive validation in my following form. The only difference I see is:
1. Line 1: I dont have it.

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<EagerlyPerformingValidation.Models.UserInformation>
  1. I should add the jquery logic to a external javascript file and reference it.

Am I missing something here??

public class Position{

            [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]   
            public int PositionID { get; set; }

            [Required(ErrorMessage = "Position name is required.")]
            [StringLength(20, MinimumLength = 3, ErrorMessage = "Name should not be longer than 20 characters.")]
            [Display(Name = "Position name")]              
            public string name { get; set; }

            [Required(ErrorMessage = "Number of years is required")] 
            [Display(Name = "Number of years")]
            [YearsValidationAttribute(5, ErrorMessage = "{0} value must be greater than {1} years.")]        
            public int yearsExperienceRequired { get; set; }

            public virtual ICollection<ApplicantPosition> applicantPosition { get; set; }
        }




@model Data.Model.Position

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Position</legend>

            <div class="editor-label">
                @Html.LabelFor(model => model.name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.name)
                @Html.ValidationMessageFor(model => model.name)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.yearsExperienceRequired)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.yearsExperienceRequired)
                @Html.ValidationMessageFor(model => model.yearsExperienceRequired)
            </div>

            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月蹉跎了容颜 2024-12-17 10:29:48

根据您的(正确)理解完成的不引人注目的验证有点“懒惰”,因为它仅在您提交表单时才会触发。您链接到的博客文章引用的 javascript 代码只会导致每次您跳出控件时触发验证。你是对的,它并不是真正不引人注目的 JavaScript。

我认为使它不引人注目的方法是向每个控件添加 data- 属性(很痛苦)并编写代码来注入脚本,或者将他的一点脚本添加到其中一个您正在引用的主要 javascript 文件,这可能是他的意图。

本书第 8 章对此有一个很酷的内容:Wrox 专业 ASP.NET MVC 3

The unobtrusive validation done according to your (correct) understanding is kind of "lazy" in that it only fires when you submit the form. The javascript code referenced by the blog post you linked to simply causes the validation to fire each time you tab out of a control. You're right that it's not really unobtrusive javascript as presented.

I think the way you could make it be unobtrusive is either to add data- attributes to each control (a pain) and write code to inject the script, or to add his little bit of script to one of the main javascript files you are referencing, which is probably his intention.

There is a cool bit about this in chapter 8 of this book: Wrox Professional ASP.NET MVC 3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文