简体   繁体   English

将jQuery验证与动态生成表单的自定义错误消息一起使用

[英]Using jQuery validation with custom error messages with dynamically-generated forms

I have a form that allows me to set values for multiple entities (books, for example) within a single HTML form. 我有一个表单,允许我在单个HTML表单中设置多个实体(例如书籍)的值。 The ASP.NET MVC view for the page takes a model that contains a List<Book> . 该页面的ASP.NET MVC视图采用包含List<Book>的模型。 This is iterated over to render the form elements for each book. 迭代这将为每本书呈现表单元素。

Let's imagine that my only validation rule is that book title is required. 让我们想象一下,我唯一的验证规则是书名是必需的。

If the user fails to enter any book titles, then I want to highlight all relevant book title textboxes using a css class and display an alert that says "Book title is required" just once. 如果用户未能输入任何书名,那么我想使用css类突出显示所有相关的书名文本框,并显示一次仅显示“需要书名”的提醒。

I'd like to use the jQuery validation plugin to achieve this. 我想使用jQuery验证插件来实现这一目标。

The documentation tells us that if we want to set a custom error message for an element we do this using the name attribute of the relevant HTML form element in a messages: section of the validate() setup call. 文档告诉我们,如果我们要为元素设置自定义错误消息,我们使用validate() setup调用的messages:部分中相关HTML表单元素的name属性来执行此操作。

eg (where my HTML element has a name of title ) 例如,(在我的HTML元素有一个name 称号

$(".selector").validate({
    rules: {
        title: "required",
    },
    messages: {
        title: "Book titles must be set",
    }
})

However, this seems incompatible with how ASP.NET MVC model binding needs to use the name attribute, when binding to a list as it makes use of the name property of each form element using a very specific syntax, eg 但是,这似乎与ASP.NET MVC模型绑定在绑定到列表时需要使用name属性的方式不兼容,因为它使用非常特定的语法来使用每个表单元素的name属性,例如

<form method="post" action="/Home/Create">

    <input type="text" name="[0].Title" value="Curious George" />
    <input type="text" name="[0].Author" value="H.A. Rey" />
    <input type="text" name="[0].DatePublished" value="2/23/1973" />

    <input type="text" name="[1].Title" value="Code Complete" />
    <input type="text" name="[1].Author" value="Steve McConnell" />
    <input type="text" name="[1].DatePublished" value="6/9/2004" />

    <input type="text" name="[2].Title" value="The Two Towers" />
    <input type="text" name="[2].Author" value="JRR Tolkien" />
    <input type="text" name="[2].DatePublished" value="6/1/2005" />

    <!-- There could be several more books in the form... -->
<input type="submit" />

(taken from Haacked ) (摘自Haacked

Are these conflicting usages of the name attribute a deal breaker as far as jQuery validation is concerned? 就jQuery验证而言,这些冲突的name属性是否属于交易破坏者?

Unfortunately, you can't use names with those characters "[0].Title" with the jquery validator for the reason you noted above. 不幸的是,由于上面提到的原因,你不能使用带有jquery验证器的字符“[0] .Title”。 Jquery validator uses the name attribute of HTML elements to build a javascript object to both set validation rules and validation messages. Jquery验证器使用HTML元素的name属性来构建javascript对象,以设置验证规则和验证消息。

One solution, would be to change the value of the name attribute prior to using jquery validator and then set it back before posting your form the server. 一种解决方案是在使用jquery验证器之前更改name属性的值,然后在将表单发布到服务器之前将其设置回来。 However, this would only work if there is nothing on the client side depending on names such as "[0].Title". 但是,这只有在客户端没有任何内容的情况下才会起作用,具体取决于诸如“[0] .Title”之类的名称。

This is what I do (reduced code for explanation) 这就是我所做的(减少代码解释)

Create a global variable to hold validation messages, called: validationMessages 创建一个全局变量来保存验证消息,称为: validationMessages

In the errorPlacement option for jquery validation I have: 在jquery验证的errorPlacement选项中,我有:

errorPlacement: function (error, element) { validationMessages += $(error).text() + '<br />'; },

Then here is my submit form function... 那么这是我的提交表单功能...

function submitForm() 
{
    validationMessages = '';

    var valid = true;

    // do more/other/custom validation and css manipulation here
    // if item not valid, set valid = false and add to validationMessages as in errorPlacement

    if ($("#MainForm").valid() && valid) 
    {
        $("#MainForm").submit();
    }
    else 
    {
        // Show Message eg: mine looks like showAlert('Please complete the form', validationMessages, '600');
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM