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

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

问题描述

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

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?

推荐答案

只需将下拉列表绑定到视图,并直接在 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 中的绑定"属性如何工作?何时以及如何使用?

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 的 required 属性应该取决于另一个组件的提交值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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