inputText的必需属性应取决于另一个组件的提交值 [英] required attribute of inputText should depend on submitted value of another component

查看:113
本文介绍了inputText的必需属性应取决于另一个组件的提交值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个下拉列表和两个输入字段的表单.

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.

我该如何实现?

推荐答案

只需将下拉列表绑定到视图,并直接在required属性中检查其值.

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'}" />

请注意,binding示例为原样.绝对不要在这里将其设置为bean属性.另请参见 JSF的'binding'属性如何工作?何时以及如何使用?

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?

还请注意,组件的顺序很重要.如果菜单位于树中输入的下方,请改用#{menu.submittedValue eq 'first'}.或者,如果您想独立于此,请改用#{param[menu.clientId] eq 'first'}.

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.

这篇关于inputText的必需属性应取决于另一个组件的提交值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆