Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF selectBooleanCheckbox required=true doesn't validate if checkbox is selected

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??

like image 747
Chetan Avatar asked Feb 10 '13 10:02

Chetan


1 Answers

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.

like image 192
Mohammad Dehghan Avatar answered Sep 28 '22 04:09

Mohammad Dehghan