3

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)

  1. user pick an option -> okay
  2. 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)

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

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
yla
  • 157
  • 2
  • 10

1 Answers1

5

Just let the required attribute of the menu check if the checkbox is checked. You can do it by binding the physical checkbox component to the view by binding attribute, which would make it to end up in an UIInput instance which in turn has getValue() method returning the submitted/converted/validated value.

<h:selectBooleanCheckbox binding="#{checkbox}" ... />
<h:selectOneMenu ... required="#{checkbox.value}" />

(note: the code is complete as-is, you do not need to bind it to a bean property!)

Note that this doesn't strictly "conditionally display the message" as you explicitly asked. It just conditionally validates the input as required. Whether the message is being displayed or not is merely a consequence.

See also:


Update: as per your attempt to check the <h:selectBooleanCheckbox value> instead, this won't work as this value is only updated during update model values phase, which is after the validations phase when the required attribute is to be checked.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Hi BalusC, if I have a checkbox , can I do something like this ??? thanks for the quick response – yla Oct 02 '13 at 18:16
  • Just use a normal JSF `` component. It generates ultimately the same HTML and it allows for much easier dynamic interweaving with the backend. – BalusC Oct 02 '13 at 18:17
  • just one more question, i am still not sure how to know if my checkbox is checked within the same jsf page (trying to get checkbox.value for my selectOneMenu). Do i need something like RequiredCheckboxValidator (from your other post) to make this work? sorry for keep asking newbie questions – yla Oct 02 '13 at 18:45
  • Where to you need to know it? In the submit method? Just bind the value to a `boolean` property in bean like so `` – BalusC Oct 02 '13 at 18:47
  • I have just update my progress, I initialized my boolean value as true, and it seems like the variable never gets changed even when the checkbox is unchecked – yla Oct 02 '13 at 22:18
  • The posted answer still applies. Add it back. Also please stop chameleonizing questions, instead just ask a new question. – BalusC Oct 02 '13 at 22:26