8

I writing JSF page, which requires users to click on a checkbox (similar to accepting the license agreement) I have following code in place:

<h:selectBooleanCheckbox value="#{checkBoxManagedBean.checkBoxValue}" required="true"  id="jsfcheckBox" >
</h:selectBooleanCheckbox>
<h:outputLabel value="accept rule label" for="jsfcheckBox" />
<h:message for="jsfcheckBox"/>
<br/>
<h:commandButton id="loginButton" value="Submit" 
   action="#{checkBoxManagedBean.testAction}"/>

I am setting the required=true, but there is no validation happening for checkbox. I dont see any message getting displayed in the page for checkbox.

I have tried f:validateRequired, even this is not working.

<h:selectBooleanCheckbox value="#{checkBoxManagedBean.checkBoxValue}" required="true"  id="jsfcheckBox" >
        <f:validateRequired for="jsfcheckBox"></f:validateRequired>
        </h:selectBooleanCheckbox>
        <h:outputLabel value="CheckBox label" for="jsfcheckBox" />
        <h:message for="jsfcheckBox"/>
        <br/>
         <h:commandButton id="loginButton" value="Submit" 
                    action="#{checkBoxManagedBean.testAction}"/>

The page doesnt have any time issue, checkbox, buttons everything visible, my expectation is getting a validation message, when checkbox is not selected before button is pressed.

BTW, is it JSF specification that required=true doesn't really execute any validation??

Chetan
  • 1,507
  • 7
  • 30
  • 43
  • required=true is validating whether value is not empty, both 'true' and 'false' are not empty values, so the validation passes. If you want the checkbox to be checked, then another type of validator should be used. – andbi Feb 10 '13 at 11:24

1 Answers1

22

required=true for jsf input fields means that the value of the field must not be empty or null. This seems like bug in jsf implementation, but when h:selectBooleanCheckbox is not checked, it's value is false and not empty or null. So this does not trigger the validation. BalusC has written a nice post about this here. He implemented a validator for this that helps you overcome the situation. Thanks BalusC.

Community
  • 1
  • 1
Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72
  • 3
    It's also in OmniFaces: http://showcase.omnifaces.org/validators/RequiredCheckboxValidator – BalusC Feb 11 '13 at 12:22