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:
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 withoutimmediate="true"
aren't processed yet at that point.In the
valueChangeListener
method associated with the dropdown, callFacesContext#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.
精彩评论