commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Masahji Stewart <masa...@citysearch.com>
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 
have
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) {
     this.name = 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 
functionality)
var windowAlertMethod = function(fieldInfoArray) {
     var alertStrings = new Array();
     var index = 0;
     for (i in fieldInfoArray) {
         alertStrings[index++] = fieldInfoArray[i].message;
     }
     alert(alertStrings.join('\n'));
}

// 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.name, 
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 
grouped
// validation methods (like the ones generated in the struts framework) 
that
// call preexisting validate methods
//
// Instead of this:     alert(fields.join('\n'));
// This:                if(customAlert) {
//                          customAlert(fields);
//                      } else {
//                          alertMethod(fields);
//                      }



-masahji


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message