Ok, I think I figured it out.
The problem is with using unobtrusive validation with MVC 3, as it initializes the jQuery validation. Thus, the only way to configure validation is to use form.data("validator").settings . However, trying to set errorLabelContainer using this method, that is:
$("#form").data("validator").settings.errorLabelContainer = "#messageBox";
... does not work, since jQuery validation uses this value internally inside the init () function to configure a set of other parameters, such as containers, etc. I tried to mimic what it was doing, or even called $("#form").data("validator").init() again after setting errorLabelContainer , but this caused strange behavior and caused a lot of other settings.
So, I took a different approach. First, I provided an MVC space for entering individual error lines using @Html.ValidationMessageFor() and adding display:none to keep it hidden:
@using (Ajax.BeginForm(...)) { <div class="field-panel"> @Html.EditorFor(m => m.PatientID) @Html.ValidationMessageFor(m => m.PatientID, null, new { style = "display:none"}) ... </div> <input type="submit" class="carecon-button-next" value="Next" /> <ul id="error-summary"> </ul> }
Then in the showErrors I will showErrors these lines into the check summary after by calling defaultShowErrors() :
$("#form").data("validator").settings.onfocusout = function (element) { $(element).valid(); }; $("#form").data("validator").settings.showErrors = function (errorMap, errorList) { this.defaultShowErrors(); $("#error-summary").empty(); $(".field-validation-error span", $("#form")) .clone() .appendTo("#error-summary") .wrap("<li>"); };
This gave me the behavior I wanted, showing / hiding validation errors as a list in the summary as the user filled in the fields on the form.
lambinator
source share