5

I have a form which contains a dropdown and two input fields.

<h:selectOneMenu />
<h:inputText />
<h:inputText />

I would like to make the required attribute of the input fields conditional depending on the selected value of the dropdown. If the user chooses the first item of the dropdown, then the input fields must be required. If the user chooses the second item, then those would not be required.

How can I achieve this?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user1213679
  • 259
  • 1
  • 4
  • 12

2 Answers2

11

Just bind the dropdown to the view and directly check its value in the required attribute.

<h:selectOneMenu binding="#{menu}" value="#{bean.item}">
    <f:selectItem itemValue="first" itemLabel="First item" />
    <f:selectItem itemValue="second" itemLabel="Second item" />
</h:selectOneMenu>

<h:inputText value="#{bean.input1}" required="#{menu.value eq 'first'}" />
<h:inputText value="#{bean.input2}" required="#{menu.value eq 'first'}" />

Note that the binding example is as-is. Do absolutely not set it to a bean property here. See also How does the 'binding' attribute work in JSF? When and how should it be used?

Also note that the ordering of the components is significant. If the menu is located below the inputs in the tree, use #{menu.submittedValue eq 'first'} instead. Or if you want to be independent from that, use #{param[menu.clientId] eq 'first'} instead.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • When the form is submitted, the selectOneMenu value will not have the new value applied when validation occurs. The `immediate` attribute on the select may get round that. – McDowell Feb 16 '12 at 12:04
  • Or by component binding, the above example assumes that the component is positioned before the inputs. – BalusC Feb 16 '12 at 12:10
  • It seems to be an easy solution but unfortunately it doesn't work. The "menu.value" is null altough the "immediate" attribute it set to true for the selectOneMenu – user1213679 Feb 16 '12 at 17:52
  • You should not set `immediate="true"` and you need to make sure that the inputs are after the menu. If it's the other way round, use `#{menu.submittedValue}` instead. – BalusC Feb 16 '12 at 17:53
  • Unfortunately it doesn't work. I paste my code:
    – user1213679 Feb 17 '12 at 10:37
  • 1
    The inputs are after the listbox, so you should use `#{pack.value}`. Further you're treating a numeric value as a string by quoting it. What's the real value type? An integer/long type? If so, remove those singlequotes: `#{pack.value == 1}`. – BalusC Feb 17 '12 at 10:40
5

Assuming you are using JSF 2.0: Let your SelectOneListBox execute with ajax and re-render the input fields on change of the list box:

A quick sketch:

<h:selectOneMenu value="#{myBean.myMenuValue}">
  <f:ajax render="input1"/>
   ..
</h:selectOneMenu>

<h:inputText id="input1" value="#{myBean.myInputValue}" 
             required="#{myBean.myMenuValue == 'firstEntry'}" />
Matt Handy
  • 29,855
  • 2
  • 89
  • 112