You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by "Don Brown (JIRA)" <ji...@apache.org> on 2006/04/25 21:17:45 UTC
[jira] Reopened: (STR-771) [validator-rules.xml] javascript
validateRequired() function does not work with radio objects
[ http://issues.apache.org/struts/browse/STR-771?page=all ]
Don Brown reopened STR-771:
---------------------------
Assign To: (was: Struts Developer Mailing List)
> [validator-rules.xml] javascript validateRequired() function does not work with radio objects
> ---------------------------------------------------------------------------------------------
>
> Key: STR-771
> URL: http://issues.apache.org/struts/browse/STR-771
> Project: Struts Action 1
> Type: Improvement
> Components: Action
> Versions: Nightly Build
> Environment: Operating System: All
> Platform: PC
> Reporter: Pierre Delisle
> Priority: Minor
> Fix For: 1.2 Family
> Attachments: validateRequired-03.txt, validateRequiredProposedPatch-focusField.txt, validateRequiredProposedPatch.txt, validator-rules.diff, validator.js
>
> The javascript validateRequired() function in validator-rules.xml does not
> work with radio objects.
> I'm no JavaScript expert, but this seems to come from the fact that
> form[oRequired[x][0]].type is undefined for a radio object (at least
> using NN 4.76 and IE 6). The radio object yields an array, and each value
> of the array must be validated to see if at least one of them has its
> checked property set to true.
> A sample page is included below, with validateRequired2() properly validating
> the radio fields. The code is quite ugly, but could not find a way in javascript
> to figure out how to test if an object is an array or not.
> ----------------------
> <html>
> <SCRIPT LANGUAGE="JavaScript">
> <!--
> function displayTypes() {
> alert(
> "textField type is: " + document.form.textField.type + "\n" +
> "textField type is: " + document.form["textField"].type + "\n" +
> "passwordField type is: " + document.form.passwordField.type + "\n" +
> "selectField type is: " + document.form.selectField.type + "\n" +
> "radioField type is: " + document.form.radioField.type + "\n" +
> "radioField type is: " + document.form["radioField"].type + "\n" +
> "radioField[0] type is: " + document.form.radioField[0].type);
> }
> function displayValues() {
> alert(
> "textField value is: " + document.form.textField.value + "\n" +
> "passwordField value is: " + document.form.passwordField.value + "\n" +
> "selectField value is: " + document.form.selectField.value + "\n" +
> "selectField selectedIndex is: " +
> document.form.selectField.selectedIndex + "\n" +
> "radioField value is: " + document.form.radioField.value + "\n" +
> "radioField[0] value is: " + document.form.radioField[0].value + "\n" +
> "radioField[0] checked is: " + document.form.radioField[0].checked);
> }
> function required () {
> this.aa = new Array("textField", "textField is required.", new Function
> ("varName", " return this[varName];"));
> this.ab = new Array("radioField", "radioField is required.", new Function
> ("varName", " return this[varName];"));
> }
> function validateRequired(form) {
> var bValid = true;
> var focusField = null;
> var i = 0;
> var fields = new Array();
> oRequired = new required();
> for (x in oRequired) {
> if ((form[oRequired[x][0]].type == 'text' ||
> form[oRequired[x][0]].type == 'textarea' ||
> form[oRequired[x][0]].type == 'select-one' ||
> form[oRequired[x][0]].type == 'radio' ||
> form[oRequired[x][0]].type == 'password') &&
> (form[oRequired[x][0]].value == '')) {
> if (i == 0) {
> focusField = form[oRequired[x][0]];
> }
> fields[i++] = oRequired[x][1];
> bValid = false;
> }
> }
> if (fields.length > 0) {
> focusField.focus();
> alert(fields.join('\n'));
> }
> return bValid;
> }
> function validateRequired2(form) {
> var bValid = true;
> var focusField = null;
> var i = 0;
> var fields = new Array();
> oRequired = new required();
> for (x in oRequired) {
> alert("object is: " + form[oRequired[x][0]].name);
> if (form[oRequired[x][0]].length == 0) {
> alert("0 length");
> } else {
> alert("not zero length: " +
> form[oRequired[x][0]].length);
> }
> if ((form[oRequired[x][0]].type == 'text' ||
> form[oRequired[x][0]].type == 'textarea' ||
> form[oRequired[x][0]].type == 'select-one' ||
> form[oRequired[x][0]].type == 'radio' ||
> form[oRequired[x][0]].type == 'password')) {
> alert("in yes");
> if (form[oRequired[x][0]].value == '') {
> if (i == 0) {
> focusField = form[oRequired[x][0]];
> }
> fields[i++] = oRequired[x][1];
> bValid = false;
> }
> } else if (form[oRequired[x][0]][0].type == 'radio') {
> alert("is radio");
> var radioChecked = false;
> for (var j=0; j<form[oRequired[x][0]].length; j++) {
> if (form[oRequired[x][0]][j].checked) {
> radioChecked = true;
> break;
> }
> }
> if (!radioChecked) {
> fields[i++] = oRequired[x][1];
> bValid = false;
> }
> } else {
> alert("in else");
> }
> }
> if (fields.length > 0) {
> focusField.focus();
> alert(fields.join('\n'));
> }
> return bValid;
> }
> -->
> </SCRIPT>
> <form name="form">
> <input type="text" name="textField" size="40" value=""></td>
> <p>
> <input type="password" name="passwordField" size="20" value=""></td>
> <p>
> <select name="selectField">
> <option value="ONE">ONE</option>
> <option value="TWO">TWO</option>
> <option value="THREE">THREE</option>
> <option value="FOUR">FOUR</option>
> <option value="FIVE">FIVE</option>
> </select>
> <p>
> <input type="radio" name="radioField" value="1">One<br>
> <input type="radio" name="radioField" value="2">Two<br>
> <input type="radio" name="radioField" value="3">Three<br>
> <p>
> <input type="button" value="Display Types" onclick="displayTypes()"/>
> <input type="button" value="Display Values" onclick="displayValues()"/>
> <input type="button" value="Validate OLD"
> onclick="validateRequired(document.form)"/>
> <input type="button" value="Validate NEW"
> onclick="validateRequired2(document.form)"/>
> </form>
> </html>
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://issues.apache.org/struts/secure/Administrators.jspa
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org