You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@struts.apache.org by Matt Rossner <ma...@gmail.com> on 2007/10/12 15:21:10 UTC
possible bug - validate radio buttons
I'm not sure if this is the correct list. I searched all over for this
problem but ended up with my own solution. I have a site using Struts and
the validator framework. We want to validate radio buttons. Since there is
no default selection for this site (customer request), the user has to pick
one. If they don't we want the validator to catch it (which it does not).
I've been able to fix this by adding some Javascript into
validation-rules.xml
Notice the difference here to account for the radio fields
if (field.type == 'text' ||
field.type == 'textarea' ||
field.type == 'select-one' ||
field.type == 'radio' ||
field.type == 'password' ||
isRadio(field)) {
if (isRadio(field)) {
var someRadioChecked = false;
for (var fieldIndex=0;
fieldIndex<field.length;fieldIndex++)
{
if (field[fieldIndex].checked) {
someRadioChecked = true;
break;
}
}
if (!someRadioChecked) {
fields[i++]=oRequired[x][1];
bValid = false;
}
} else {
var value;
// get field's value
if (field.type == "select-one") {
var si = field.selectedIndex;
value = field.options[si].value;
} else {
value = field.value;
}
if (value == '') {
if (i == 0) {
focusField = field;
}
fields[i++] = oRequired[x][1];
bValid = false;
}
}
}
I also had add this function
function isRadio(obj) {
//returns true if is an array of radio buttons
if (obj.length > 0) {
if (obj[0].type == 'radio') {
return true;
}
}
return false;
}
Hopefully this is useful to someone else who runs into this problem. Feel
free to use it.
Matt
Re: possible bug - validate radio buttons
Posted by Antonio Petrelli <an...@gmail.com>.
Matt,
I cannot understand if yours is a request for support or a bug that
you have found.
In the first case, please ask the Struts Users mailing list:
http://struts.apache.org/mail.html
In the second case (a bug), file a JIRA issue:
http://issues.apache.org/struts/
The "issues@" mailing list is only used by JIRA to announce variations
on the JIRA instance.
Ciao
Antonio
2007/10/12, Matt Rossner <ma...@gmail.com>:
> I'm not sure if this is the correct list. I searched all over for this
> problem but ended up with my own solution. I have a site using Struts and
> the validator framework. We want to validate radio buttons. Since there is
> no default selection for this site (customer request), the user has to pick
> one. If they don't we want the validator to catch it (which it does not).
> I've been able to fix this by adding some Javascript into
> validation-rules.xml
>
> Notice the difference here to account for the radio fields
>
>
> if (field.type == 'text' ||
> field.type == 'textarea' ||
> field.type == 'select-one' ||
> field.type == 'radio' ||
> field.type == 'password' ||
> isRadio(field)) {
>
> if (isRadio(field)) {
> var someRadioChecked = false;
>
> for (var fieldIndex=0;
> fieldIndex<field.length;fieldIndex++)
> {
> if (field[fieldIndex].checked) {
> someRadioChecked = true;
> break;
> }
> }
>
> if (!someRadioChecked) {
> fields[i++]=oRequired[x][1];
> bValid = false;
> }
>
> } else {
>
>
>
> var value;
> // get field's value
> if (field.type == "select-one") {
> var si = field.selectedIndex;
> value = field.options[si].value;
> } else {
> value = field.value;
> }
>
> if (value == '') {
>
> if (i == 0) {
> focusField = field;
> }
> fields[i++] = oRequired[x][1];
> bValid = false;
> }
> }
> }
>
>
>
> I also had add this function
>
> function isRadio(obj) {
> //returns true if is an array of radio buttons
> if (obj.length > 0) {
> if (obj[0].type == 'radio') {
> return true;
> }
> }
> return false;
> }
>
> Hopefully this is useful to someone else who runs into this problem. Feel
> free to use it.
>
> Matt
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org