Well, I figured out a nice way to do this in 2.0. It may seem like a
hack, but it seems fairly clean to me. I only tested this on IE7 and
Firefox 2.0 and on a simple page with two validation groups, but it
seems to work nicely.
Basically, you need to hijack two javascript functions in the UI
validation resource...ValidatorUpdateIsValid() and
ValidationSummaryOnSubmit(). To view the original, just navigate to
the second <script> AXD file in your resulting page source (view
source and paste into address bar). You'll have to save the AXD file,
and then open it in notepad to see the actual javascript.
Copy the two functions you want to hijack (ValidatorUpdateIsValid()
and ValidationSummaryOnSubmit()) into a new js file and then use
ClientScript.RegisterStartupScript to register a <script
src='hijack.js' type='text/javascript'></script> tag in the document.
This must appear after the script tag that registers the UIValidation
resource which is why I used RegisterStartUpScript and not
RegisterClientScriptBlock. You need to do this on every page you want
this behavior. I'd suggest creating either a base page class or
utility class function.
The changes you make to these functions are pretty simple:
function ValidatorUpdateIsValid() {
Page_IsValid = AllValidatorsValid(Page_Validators);
refreshSummaries(); // <--- I ADDED THIS LINE
}
function ValidationSummaryOnSubmit(validationGroup) {
.... (CODE UNCHANGED AND OMITTED FOR THIS POST)
// summary.style.display = "none"; <-- I COMMENTED OUT THIS
LINE
.... (CODE UNCHANGED AND OMITTED FOR THIS POST)
// I ADDED THE THIRD CONDITION IN THE FOLLOWING BLOCK
s += first;
for (i=0; i<Page_Validators.length; i++) {
if (!Page_Validators.isvalid &&
typeof(Page_Validators.errormessage) == "string" &&
Page_Validators.validationGroup == validationGroup) {
s += pre + Page_Validators.errormessage +
post;
}
}
.... (CODE UNCHANGED AND OMITTED FOR THIS POST)
}
I didn't like having to hijack the previous function because of its
size, but it was the only way to get the behavior I wanted.
Then add two new functions:
function refreshSummaries() {
if (typeof(Page_ValidationSummaries) == "undefined") {
return;
}
for (var i=0;i<Page_ValidationSummaries.length;i++) {
if (isGroupValid(Page_ValidationSummaries)) {
// hide
Page_ValidationSummaries.style.display = "none";
} else {
// Refresh
ValidationSummaryOnSubmit(Page_ValidationSummaries.validationGroup);
}
}
}
function isGroupValid(summary) {
if (typeof(Page_Validators) == "undefined") {
return true;
}
var summaryGroupName;
var validatorGroupName;
if ((typeof(summary.validationGroup) == "undefined") ||
(summary.validationGroup == null)) {
summaryGroupName = '';
} else {
summaryGroupName = summary.validationGroup;
}
for (var i=0;i<Page_Validators.length;i++) {
if ((typeof(Page_Validators.validationGroup) ==
"undefined") || (Page_Validators.validationGroup == null)) {
validatorGroupName = '';
} else {
validatorGroupName = Page_Validators.validationGroup;
}
if (validatorGroupName == summaryGroupName) {
if (!Page_Validators.isvalid) {
return false;
}
}
}
return true;
}
What my new validation code does is update the summaries at the same
time the individual validators are fired. That way, when you leave a
required field, you can see the "*" text from the validation control
as well as the real error message "Field A is required" in the
summary. It also changes the default behavior where only one summary
is shown at a time. If you have multiple summaries, they stay visible
if you have failed validations. The default behavior, however, is
still exhibited when you try to submit a form (only the val summary of
the applicable group is displayed).
If you have any questions or want to share ideas or report bugs,
contact me at jjbutera-AT-HOTMAIL-DOT-COM.