This situation happens when the checkboxes and/or radio buttons are
dynamically generated from a database or somewhere. You won't know for
sure that there are going to be more than one checkboxes and radio
buttons, sometimes, you just get one. But in your application, you want
to have a consistent implementation and make them required regardless. I
just ran into this situation.

 
Saul 

-----Original Message-----
From: Jayaraman Dorai [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, October 29, 2003 2:00 PM
To: Struts Users Mailing List
Subject: RE: validator for check box

Been using an older version of validator-rules, which I had customized
and so the multi-check box wasn't working.

Required for a single-checkbox is not need for an application, since you
are compelling the user to have that one choice selected.  If you have
only one check box and that too is required, you need not ask an user to
enter them.

> -----Original Message-----
> From: Saul Q Yuan [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, October 29, 2003 1:06 PM
> To: 'Struts Users Mailing List'
> Subject: RE: validator for check box
> 
> 
> I just submitted a bug and submitted a patch as well for this problem.
> Basically, the validator (javascript part) works fine for multiple
> checkboxes and radio buttons, but doesn't for a single checkbox or a
> single radio button. I modified the validateRequired method as below,
> you'll need to replace this method in the validator-rules.xml file.
> Works fine for me.
> 
> Saul
> 
> 
> -------------------------------------------------
>             function validateRequired(form) {
>                 var isValid = 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' || // -- true for single
> radio button, Saul Q Yuan ([EMAIL PROTECTED]) 10/28/03
>                         field.type == 'checkbox' || // -- true for
> single checkbox, Saul Q Yuan ([EMAIL PROTECTED]) 10/28/03
>                         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;
>                             }
>                                               // -- get value for
> checked single radio button or checkbox, Saul Q Yuan
> ([EMAIL PROTECTED]) 10/28/03
>                                               } else if (field.type ==
> "radio" || field.type == "checkbox") {
>                                                       if
> (field.checked) {
>                                                               value =
> field.value;
>                                                       }
>                         } else {
>                             value = field.value;
>                         }
>                         
>                         if (trim(value).length == 0) {
>                         
>                             if (i == 0) {
>                                 focusField = field;
>                             }
>                             fields[i++] = oRequired[x][1];
>                             isValid = false;
>                         }
>                     } else if (field.type == "select-multiple") { 
>                         var numOptions = field.options.length;
>                         lastSelected=-1;
>                         for(loop=numOptions-1;loop>=0;loop--) {
>                             if(field.options[loop].selected) {
>                                 lastSelected = loop;
>                                 value = field.options[loop].value;
>                                 break;
>                             }
>                         }
>                         if(lastSelected < 0 || 
> trim(value).length == 0)
> {
>                             if(i == 0) {
>                                 focusField = field;
>                             }
>                             fields[i++] = oRequired[x][1];
>                             isValid=false;
>                         }
>                     } else if ((field.length > 0) && (field[0].type ==
> 'radio' || field[0].type == 'checkbox')) {
>                         isChecked=-1;
>                         for (loop=0;loop < field.length;loop++) {
>                             if (field[loop].checked) {
>                                 isChecked=loop;
>                                 break; // only one needs to be checked
>                             }
>                         }
>                         if (isChecked < 0) {
>                             if (i == 0) {
>                                 focusField = field[0];
>                             }
>                             fields[i++] = oRequired[x][1];
>                             isValid=false;
>                         }
>                     }
>                 }
>                 if (fields.length > 0) {
>                    focusField.focus();
>                    alert(fields.join('\n'));
>                 }
>                 return isValid;
>             }
> 
> --------------------------------------------------------
> 
> 
> 
> 
> -----Original Message-----
> From: Jayaraman Dorai [mailto:[EMAIL PROTECTED] 
> Sent: Wednesday, October 29, 2003 12:49 PM
> To: Struts Users Mailing List
> Subject: RE: validator for check box
> 
> Does any one have any ideas or have written code to validate that at
> least one item is selected on the  check box? Would be 
> interested in the
> javascript code for the same. On the server side, I can do that
> validation on the action form, though doing it through struts 
> validator
> will be the ideal. Wondering why struts doesn't have it? Again, am I
> missing something?
> 
> Thanks
> Jayaraman 
> 
> > -----Original Message-----
> > From: Jayaraman Dorai 
> > Sent: Monday, October 27, 2003 2:31 PM
> > To: [EMAIL PROTECTED]
> > Subject: validator for check box
> > 
> > 
> > Would like to validate that the user selects at least one 
> > option in the check box which was created using 
> > html-multibox. The struts-validator is not validating the 
> > required for a check box.  Is there any code for validating 
> > the check box or am I missing something?
> >  
> > Thanks
> > Jayaraman
> > 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to