xVal and list inputs

Jun 4, 2010 at 8:53 PM

When you have something like this:

 

                        <% for (var i = 0; i < 10; i++ ) { %>
                        <tr>
                            <td>
                                <%= Html.LabelFor(m => m.DomainTraining[i].Hours) %>
                                <%= Html.CustomTextBoxFor(m => m.DomainTraining[i].Hours, new { style = "width:50px;" })%>
                                <%= Html.ValidationMessageFor(m => m.DomainTraining[i].Hours)%>
                            </td>
                            <td>
                                <%= Html.LabelFor(m => m.DomainTraining[i].Description) %>
                                <%= Html.CustomTextBoxFor(m => m.DomainTraining[i].Description) %>
                                <%= Html.ValidationMessageFor(m => m.DomainTraining[i].Description)%>
                            </td>
                            <td>
                                <%= Html.LabelFor(m => m.DomainTraining[i].CompleteDate) %>
                                <%= Html.CustomTextBoxFor(m => m.DomainTraining[i].CompleteDate, new { style = "width:150px;" })%>
                                <%= Html.ValidationMessageFor(m => m.DomainTraining[i].CompleteDate)%>
                                <%= Html.ClientSideValidation<DomainTraining>("DomainTraining[" + i + "]") %>
                            </td>
                        </tr>
                        <% } %>

 

And you want to attach a client side validation thing to each one (As I tried to do above) it doesn't attach the validator correctly (MVC2). I don't know if this is because of a change in MVC2 or not, but the client side ID of these items would be DomainTraining_x__Hours, etc. xVal attempts to attach it to DomainTraining[x]_Hours, which while that makes sense, is incorrect. The fix would be one line 53 of the library:

 

            return fullyQualifiedModelName.replace(/[\.\[\]]/g, "_");

All I did was add the brackets to the RegEx to be replaced by the "_" character. Validators now attach correctly. If there was a reason for this, please advise.

/ Michael /