Input text validation based on drop-down list selection

自闭症网瘾萝莉.ら 提交于 2019-12-24 00:50:01

问题


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


回答1:


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

E.g.

<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>

with

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

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

}

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.



来源:https://stackoverflow.com/questions/8263909/input-text-validation-based-on-drop-down-list-selection

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