基于下拉列表选择的输入文本验证 [英] Input text validation based on drop-down list selection

查看:66
本文介绍了基于下拉列表选择的输入文本验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据下拉列表中的选择来验证输入文本框?

How can I validate an input text box based on a selection from the drop-down list?

推荐答案

您可以将下拉列表的选定值作为输入组件的属性传递,以便验证程序可以获取它.

You could pass the selected value of the dropdown as an attribute of the input component so that the validator can grab it.

例如

<h:selectOneMenu binding="#{menu}" value="#{bean.item}">
    <f:selectItems value="#{bean.items}" />
</h:selectOneMenu>
<h:inputText value="#{bean.input}">
    <f:attribute name="item" value="#{menu.value}" />
    <f:validator validatorId="inputValidator" />
</h:inputText>

使用

@FacesValidator("inputValidator")
public class InputValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) {
        Object item = component.getAttributes().get("item");
        // ...
    }

}

请注意,组件的顺序很重要. JSF按顺序处理 UIInput 组件它们出现在视图中.如果将下拉组件放置在输入文本组件的之后,则需要将#{menu.submittedValue}作为属性传递,但此时该值尚未转换.如有必要,可以使用<h:inputHidden>解决方法,该方法放在两个组件之后,并将验证器放在其中.

Note that the ordering of the components matters. JSF processes UIInput components in the order they appear in the view. If the dropdown component is placed after the input text component, then you need to pass #{menu.submittedValue} as attribute, but at that point the value is not converted yet. You could if necessary workaround with a <h:inputHidden> which is placed after the both components and put the validator in there.

这篇关于基于下拉列表选择的输入文本验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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