changed
changed

Reputation: 2143

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

Upvotes: 1

Views: 3050

Answers (1)

BalusC
BalusC

Reputation: 1109655

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.

Upvotes: 1

Related Questions