commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Masahji Stewart <>
Subject Javascript Validation Flexibility
Date Wed, 18 Feb 2004 20:50:18 GMT
I ran into a roadblock in the validator framework when I wanted to 
create a
custom alert that didn't involve the window popup "alert" function (ie 
a DHTML implementation).

I think it would be extremely useful to allow users to declare their 
own alert
functions that have access to more details about what exactly went 
wrong in the
chain of javascript validation events.

So, below I have described a potential solution to this problem that I 
tested and verified for backwards compatibility.

// first create a custom javascript object that encapsulates the field
// information for fields that didn't validate
function FieldInfo(name, value, message) { = name;
     this.value = value;
     this.message = message;

// declare a variable that acts as a callback to the alert method
var alertMethod;

// this method captures the current alert functionality (default 
var windowAlertMethod = function(fieldInfoArray) {
     var alertStrings = new Array();
     var index = 0;
     for (i in fieldInfoArray) {
         alertStrings[index++] = fieldInfoArray[i].message;

// alertMethod will be windowAlertMethod by default
alertMethod = windowAlertMethod;

// validate method changes:

// All of the validate methods (ie validateRequired, validateMask,..) 
will have
// to take in an extra  parameter, the customAlertMethod
// Instead of this:     function validateRequired(form) { ... }
// This:                function validateRequired(form, customAlert) { 
... }

// All of the methods should create an array of FieldInfo objects 
instead of
// string messages
// Instead of these:    fields[i++] = oRequired[x][1];
// These:               fields[i++] = new FieldInfo(, 
field.value, oRequired[x][1]);

// Finally, the call to alert will change to the customAlert call
// The check provides backwards compatibility with the single parameter
// validate method calls, but also allows us to create fully custom 
// validation methods (like the ones generated in the struts framework) 
// call preexisting validate methods
// Instead of this:     alert(fields.join('\n'));
// This:                if(customAlert) {
//                          customAlert(fields);
//                      } else {
//                          alertMethod(fields);
//                      }


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message