On my JSF page, I have a HTML input checkbox where allows users to choose if they want the gift or not, and the <div>
section under the checkbox information will only be displayed if the checkbox is checked. Once the checkbox is checked, users will be required to select an option (from a dropdown menu, initial value of dropdown menu = null); however, the selection is not required if the checkbox is not checked.
So, here comes two conditions:
A. Checkbox is checked ( == div view is played)
- user pick an option -> okay
user do not select anything -> I want the required message to be displayed, and this is what I have done:
<p:message style="margin: 10px" id="messages7" for="npsScoreSupport" /> <h:selectOneMenu id="npsScoreSupport" value="#{npsBean.supportScore}" required="true" requiredMessage="Please select an option"> <f:selectItems value="#{npsBean.ratingPickList}" /> </h:selectOneMenu>
B. Checkbox is unchecked (== div view is hidden)
- User do not selection anything -> okay, but the page still requires users to pick an option as I have the required= "true".
Since I need the requireMessage to make sure an option is picked when the checkbox is checked, I am wondering if it is possible to make the requireMessage conditional based on the state of checkbox? (require when checkbox is checked, and not required when unchecked) any suggestion?
============updated======================
So now I have updated my checkbox and connects it with a function in JAVA class (boolean variable check = true as default)
<h:selectBooleanCheckbox class="someClass" value="#{someBean.check}" />
And here is my selector
<h:selectOneMenu id="OptSelector" value="#{someBean.Opt}" required="#{someBean.check}" requiredMessage="Please select and option">
<f:selectItems value="#{npsBean.OptPickList}" />
The submission failed even when the checkbox is unchecked, it seems like the boolean check never changes. Anyone know why?