commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 32343] - [validator] Javascript Rendering Extension
Date Fri, 26 Nov 2004 17:18:56 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG·
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=32343>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND·
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=32343





------- Additional Comments From nacho@visual-ma.com  2004-11-26 18:18 -------
I would like to suggest a different way of doing things with the client side
validation. If we make the different validators validate values instead of
fields, we would improve in code reusability and duplication. I'll try to explain:

Use a generic javascript function called grabValuesFromField(oField) (or
whatever) which returns an array (most of the times a 1 element array) of
strings with all values of the specified field. Then have the different
validators validate all values from the array making them easier to mantain and
more reusable. 

There is a common misconception when using html that states that the name of a
text field must be unique within a form. This is because IMHO javascript deals
with this issue erroneously: document.forms[0].textField.value should be
document.forms[0].textField[0].value. Most of the time there is one text field
for the same name, but with checkboxes or radio buttons this is not true. If you
use several text fields with the same name (which is absolutely necessary
sometimes) you need to loop through in a document.forms[0].textField[i] fashion
just like radios and checkboxes.

This is the main reason why radio and checkbox validations perform poorly
usually. I propose a first draft for this function:

function grabValuesFromField(oField) {
	var values = new Array();
	if ((oField.length) && (!oField.type || oField.type.indexOf('select') == -1)) {
		//we have to push out the selects when they have a type because select.length
refers to options
		for (n=0; n<oField.length; n++) {
			var singleValues = grabValuesFromSingleField(oField[n]);
			name = oField[n].name;
			values = values.concat(singleValues);
		}		
	} else {
		values = grabValuesFromSingleField(oField);
	}
	alert(values.length);
}

function grabValuesFromSingleField(oSingleField) {
	var singleValues = new Array();
	if (oSingleField.type == 'select-multiple') {
		for (s=0; s<oSingleField.options.length; s++) {
			if (oSingleField.options[s].selected) {
				singleValues[singleValues.length] = oSingleField.options[s].value;
			}
		}
	} else if (oSingleField.type == 'select-one') {		
		singleValues[0] = oSingleField.options[oSingleField.selectedIndex].value;
	} else if (oSingleField.type == 'radio' || oSingleField.type == 'checkbox') {		
		if (oSingleField.checked) {
			singleValues[singleValues.length] = oSingleField.value;
		}
	} else {
		singleValues[0] = oSingleField.value;
	}
	return singleValues;
}

Please take a look to the following page so you can check this in action:
http://www.visual-ma.com/validator/grab.html 
The page is composed of two panels one with single fields and one with double
fields. If you click on grab you will get the number of values it has returned.
This is the number of values that should be passed to the validators. I know the
page is ugly and maybe quite confusing, specially after seeing Niall's... If
requested I'll improve it so we could have pseudo test cases.

Nacho G. Mac Dowell

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

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