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?

2 Answers 2

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.

6
  • 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, 2012 at 12:04
  • Or by component binding, the above example assumes that the component is positioned before the inputs.
    – BalusC
    Feb 16, 2012 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 Feb 16, 2012 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, 2012 at 17:53
  • 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, 2012 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'}" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.