Is the javascript WebUiValidation.js no longer available for viewing in 2.0?

D

daokfella

It looks like this is now an embedded resource. So is it permanently
hidden?

In earlier versions of .Net, I made a simple change to the
WebUiValidation.js so that the validation summary was refreshed
whenever a validator fired on the client side. That way, if my
validator text was simply "*", I didn't have to hit the submit button
to see the actual message "Last Name is required" in the summary. It
was a slick little improvement, but it looks like I can't incorporate
this modification into my 2.0 project. Or is there?

Is it me, or does it not seem that my modification should be the
default behavior for the summary control anyway?

Jason
 
D

daokfella

OK, I navigated to the axd file, and saved it to my machine. I can now
at least see the functions being run. I'm hoping to be able to hijack
a function so I can use my custom script to automatically update the
summary control. I'll keep you guys posted in case you're interested
in doing the same thing.
 
D

daokfella

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.
 
D

daokfella

I've fixed some bugs that arise when using validators with
validationgroups along with others without a validationgroup. My
previous post should at least help you out. E-mail me for the updated
js file if you're interested.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top