The moose likes JSF and the fly likes Mandatory check for select boolean check box Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » JSF
Reply Bookmark "Mandatory check for select boolean check box" Watch "Mandatory check for select boolean check box" New topic
Author

Mandatory check for select boolean check box

prasad kolla
Greenhorn

Joined: Jul 19, 2008
Posts: 12
hi,

we are using trinidad tags in our application. I have a requirement where i need to do a requiredness validation if the user does not check a check box.

Normally when we specify required="true" for a input text or combobox. it shows a validatation message if the user does not enter a valid text or does not select any thing from the drop down.

Please find the below code snippet:

<tr:selectBooleanCheckbox id="offsiteInterview" required="true" selected="false"
label="#{msgs.input_label_checkBox}" autoSubmit="true"
value="#{captureBusinessEvidenceStep.isOffsiteInterview}"
requiredMessageDetail="option must be selected." />

Please suggest.
Bindesh Nair
Greenhorn

Joined: Oct 09, 2007
Posts: 12
Tried the same code, yes, required= true does not help, the form gets posted.

Consider using a java script instead.
prasad kolla
Greenhorn

Joined: Jul 19, 2008
Posts: 12
I cannot use java script in my application. I have tried assigining a null value and check for required ness even then no luck. I need it this asap.
Bindesh Nair
Greenhorn

Joined: Oct 09, 2007
Posts: 12
I tried the following with JSF1.2 Mojarra build and it worked for me.

The JSF code looks like this



And in the backing bean, I had the following code


[ September 19, 2008: Message edited by: Bindesh Nair ]
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
The 'required' attribute of the selectBooleanCheckbox is indeed a bit ambiguous and non-intuitive.

It will only check if the checkbox has sent any value or not. It would only display the required message if the value is null. But an unchecked checkbox by default already sends 'false' to the server and not null. With other words, the 'required' attribute is fairly useless in case of a selectBooleanCheckbox.

If you want to display the required message when the user sends the value 'false', then you indeed need to create a validator yourself.

A more clean solution than the above proposed validator is this one. It handles validation as it should be and, above all, it makes use of the default required message:

Use it as follows:
You can of course also define it as a standalone Validator implementation with a validatorId in the faces-config.xml, so that you can reuse it on other pages which uses another backing beans.
[ September 19, 2008: Message edited by: Bauke Scholtz ]

Code depot of a Java EE / JSF developer | JSF / Eclipse / Tomcat kickoff tutorial | DAO kickoff tutorial | I ♥ Unicode
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Mandatory check for select boolean check box
 
Similar Threads
Inline Form Validation
CheckBox state
select all option in JSP
HOw to make Jcombobox mandatory?
JSF - Bean level form validation for listbox - required = "true" is not working properly