struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joe Germuska <...@Germuska.com>
Subject JavaScript bug in validateRequired?
Date Mon, 31 Mar 2003 23:28:30 GMT
I'm no JavaScript wizard, so I thought i'd post this before filing a bug.

There seems to be a bug in the step which dereferences an element of 
the form to test to see if it has a value.   With some judicious use 
of JavaScript alert debugging, I can see that there's a problem with 
this statement:

     var field = form[oRequired[x][0]];

if the "subscript" to "form" is the name of a radio button form 
property, then the return value (assigned to 'field') is not the kind 
of thing which has a "type" -- it's a "Collection".  Since it doesn't 
have a "type", the test in the next line never evaluates to true, and 
so the client-side test of whether some value has been selected for 
that form element is never performed.

So, I've figured this much out, but I'm not sure how to fix it. 
JavaScript doesn't seem to have an "instanceof" operator, so what is 
the test to see whether the thing named "field" is a Collection or a 
simple element?  Presumably once you know it's a Collection, it 
wouldn't be hard to iterate through it and make sure at least one of 
its members has a value, satisfying the "required" rule, or if not, 
to add an error to the "fields" array of error messages.

Anyone care to advise?  It looks like this flaw may affect other 
javascript chunks, since I saw a few other places where this kind of 
test is executed.

Joe

for reference, the javascript from "validator-rules.xml":

function validateRequired(form) {
   var bValid = true;
   var focusField = null;
   var i = 0;
   var fields = new Array();
   oRequired = new required();
   for (x in oRequired) {
     var field = form[oRequired[x][0]];
     if (field.type == 'text' ||
         field.type == 'textarea' ||
         field.type == 'file' ||
         field.type == 'select-one' ||
         field.type == 'radio' ||
         field.type == 'password') {

           var value = '';
           // get field's value
           if (field.type == "select-one") {
             var si = field.selectedIndex;
             if (si >= 0) {
               value = field.options[si].value;
             }
           } else {
             value = field.value;
           }

           if (value == '') {
             if (i == 0) {
                 focusField = field;
             }
             fields[i++] = oRequired[x][1];
             bValid = false;
           }
       }
   }
   if (fields.length > 0) {
      focusField.focus();
      alert(fields.join('\n'));
   }
   return bValid;


-- 
--
Joe Germuska
Joe@Germuska.com
http://blog.germuska.com
"If nature worked that way, the universe would crash all the time."
	--Jaron Lanier

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


Mime
View raw message