h:selectOneMenu onchange =" submit()";立即=“真"不跳过其他输入的验证 [英] h:selectOneMenu onchange="submit()" immediate="true" does not skip validation of other inputs
问题描述
我不能将我的h:selectOneMenu
设置为在不验证其他输入的情况下立即提交.这是代码:
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?
推荐答案
当前输入组件上的immediate="true"
不会阻止其他输入组件的验证器触发.这只会导致当前输入组件的验证比通常提前一个阶段.基本上,您需要将valueChangeListener
附加到输入组件,然后在侦听器方法中调用FacesContext#renderResponse()
,以便跳过所有其他输入组件的处理.
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.
但是当您已经在使用JSF 2.0时,更好/更容易的是使用ajax功能来代替这种老式的JSF 1.x方法.
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.
例如
<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>
使用
public void changeCountry() {
System.out.println("Selected country is: " + country);
}
如果您希望在更改选择内容时重新呈现表单的某些部分,请使用render
属性.例如
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" />
另请参见:
- 何时使用valueChangeListener或f:ajax监听器? /a>
这篇关于h:selectOneMenu onchange =" submit()";立即=“真"不跳过其他输入的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!