开发者

Avoid required=true constraint of an input field when changing selection in dropdown

I have a dropdown and a text field next to it. Based on value selected in dropdown I am changing the type of text field, like change it to date, integer, text. Those text fields have required attribute set to true.

So when I select a different value in dropdown, I can change the type of text field but I also get a required error message on the text field. How can I avoid this?

I am using JSF 1.2.

<h:selectOneMenu id="SelectField"  
    value="#{logSearchBean.searchType}"    
    onchange="this.form.submit();"
    valueChangeListener="#{logSearchBean.searchValueType}" >
    <f:selectItems  value="#{logSearchBean.columnDesc}" />
</h:selectOneMenu>

<h:inputText id="SearchText"
    value="#{logSearchBean.searchValue}"
    required="true"
    requiredMessage="Plea开发者_运维百科se provide value to Search for"
    rendered="#{logSearchBean.searchValueEditor eq 'SearchText'}"/>

<t:inputDate id="SearchDate"
    value="#{logSearchBean.searchValueDate}"
    popupCalendar="true"
    required="true"
    requiredMessage="Please provide value to Search for"
    rendered="#{logSearchBean.searchValueEditor eq 'SearchDate'}"/>


You need to do two things:

  1. Add immediate="true" to the dropdown.

    <h:selectOneMenu immediate="true">
    

    This will cause the valueChangeListener method being invoked during apply request values phase instead of the validations phase. Thus, it will be invoked one phase earlier than usual and all other input fields without immediate="true" aren't processed yet at that point.

  2. In the valueChangeListener method associated with the dropdown, call FacesContext#renderResponse().

    FacesContext.getCurrentInstance().renderResponse();
    

    This will instruct JSF to bypass all remaining phases until the render response phase. Thus, the other input fields without immediate="true" won't be processed anymore.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜