JSF selectBooleanCheckbox required = true does not check if the checkbox is checked

I am writing a JSF page that requires users to click on a checkbox (similar to accepting a license agreement) I have the following code:

<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 set the value required = true, but the check for the checkbox fails. I don’t see the message being displayed on the page.

I tried f: validateRequired, even this does not work.

 <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 does not have time, a flag, all buttons are visible, my expectation receives a verification message when the flag is not selected before the button is clicked.

BTW, is this a JSF specification that is required = true does not perform any validation?

+6
source share
1 answer

required=true for jsf input fields means that the field value must not be empty or zero. This seems like an error in the jsf implementation, but when h:selectBooleanCheckbox not checked, the value is false and not empty or null . Thus, this does not trigger a check. BalusC wrote a good post about this here . He has implemented a validator for this, which will help you overcome the situation. Thanks BalusC .

+17
source

All Articles