Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

jsf 2 - h:selectOneMenu onchange="submit()" immediate="true" does not skip validation of other inputs

I can't set my h:selectOneMenu to submit immediately without validating other inputs. Here is the code:

<h:selectOneMenu value="#{a.avalue}" onchange="submit()" immediate="true">   
    <f:selectItems value="#{b.bvalue}" var="k" itemLabel="#{k.asdad}"  
        itemValue="#{k.asdad}"/>   
</h:selectOneMenu>   
<h:inputText id="sada" value="#{c.cvalue}" required="true"  
    requiredMessage="*asdadadadasd"  
    validatorMessage="*asdsadadadadad">   
    <f:validateLength maximum="80"/>   
</h:inputText>

When I change the menu value, the validators of other inputs still fire. How can I deny that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The immediate="true" on current input component doesn't stop validators of other input components from firing. It only causes that the current input component will be validated one phase before than usual. Basically, you need to attach a valueChangeListener to the input component and then call FacesContext#renderResponse() in the listener method so that the processing of all other input components will be skipped.

But as you're already using JSF 2.0, much better/easier is to use ajax powers instead of this old fashioned JSF 1.x approach.

E.g.

<h:selectOneMenu value="#{bean.country}">
    <f:selectItems value="#{bean.countries}" var="country" itemLabel="#{country.name}" itemValue="#{country.code}"/>
    <f:ajax listener="#{bean.changeCountry}" />
</h:selectOneMenu>   

with

public void changeCountry() {
    System.out.println("Selected country is: " + country);
}

If you'd like to re-render some parts of the form whenever the selection is changed, then use the render attribute. E.g.

    <f:ajax listener="#{bean.changeCountry}" render="otherComponentId" />

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...