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

Reply via email to