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." />
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 ]