• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I require at least one boolean checkbox to be checked?

 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm using MyFaces 1.1.6 with JBoss 5.1. On a particular JSF page, I have three checkboxes that are displayed like the one below ...

<h:selectBooleanCheckbox onclick="toggleTourView(this);" value="#{capitolToursController.requestingDomeTour}" id="domeTour" />

I would like to impose a validation rule whereby at least one of the checkboxes must be checked. Could someone provide an example of how this is done?

Thanks,- Dave


 
Tim Holloway
Bartender
Posts: 18415
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The technical term for this kind of checkbox is "Radio Button".

If you use radio buttons, you get that kind of behavior from your web browser itself even without using advanced frameworks like JSF.

Of course, you could force the same behavior on checkboxes, but that would require a validator that knew which checkboxes to cross-check, and a trip down to the server to do the validation. HTML radio buttons fo their magic without the need for any server interaction at all, so no server delays or extra logic required.
 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't want radio buttons because we want to give users the option to select multiple checkboxes in the set. You mention a validator -- is that something I would write myself or is there anything pre-built that would do the job? I'm using MyFaces 1.1.6.

Thanks, -
 
Tim Holloway
Bartender
Posts: 18415
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Blind as usual. Missed the "At least one" part.

No, there's no stock validator I know of for that, but it's easy to write your own. A validator is a simple class that has one primary method (validate) and throws a ValidationException if the item(s) in question are not valid. The only thing really tricky about it is that you have to have a way to indicate which controls the validator will work on.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!