I have a form that allows me to set values for multiple entities (books, for example) within a single HTML form. The ASP.NET MVC view for the page takes a model that contains a 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.
I'd like to use the jQuery validation plugin to achieve this.
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.
e.g. (where my HTML element has a name of title)
$(".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, e.g.
<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)
Are these conflicting usages of the name attribute a deal breaker as far as jQuery validation is concerned?