required attribute of inputText should depend on submitted value of another component

落爺英雄遲暮 提交于 2019-11-30 07:31:57
BalusC

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.

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'}" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!