【发布时间】:2014-04-01 04:39:50
【问题描述】:
我有一个重置按钮,点击它会触发 f:converter 和 f:validate ,这很有意义,因为它是为了清除字段。
我正在阅读this article,它演示了当我使用immediate="true" 单击“重置”按钮时如何防止出现验证,这非常有效……仅适用于第一个重置按钮。
但是,我有多个重置按钮并将它们都设置为属性immediate="true",但它似乎只适用于第一个重置按钮,我仍然收到错误消息显示其他 3 个重置按钮的无效数据。
如果需要布局背景,我的 XHTML 看起来像这样:
<a4j:outputPanel ajaxRendered="true">
<h:messages globalOnly="true" showDetail="false" infoClass="info" warnClass="warning" errorClass="error" fatalClass="fatal" />
<h:message for="field1" errorClass="error"></h:message>
<h:message for="field2" errorClass="error"></h:message>
<h:message for="field3" errorClass="error"></h:message>
<h:message for="field4" errorClass="error"></h:message>
</a4j:outputPanel>
<h:form id="form1" rendered="true">
<h:panelGrid columns="8">
<h:outputText value="#{bundle.label}" />
<h:inputText id="field1" size="18" maxlength="18" immediate="true"
value="#{myBean.myField1}">
<f:converter converterId="LongConverter" />
<f:validateLength minimum="1" maximum="18"></f:validateLength>
</h:inputText>
</h:panelGrid>
<h:commandButton id="reset1" type="submit" value="#{bundle.label}" onclick="resetField()"
styleClass="button" action="#{myBean.reset1}" immediate="true" />
</h:form>
<h:form id="form2" rendered="true">
<h:panelGrid columns="8">
<h:outputText value="#{bundle.label}" />
<h:inputText id="field2" size="18" maxlength="18" immediate="true"
value="#{myBean.myField2}">
<f:converter converterId="LongConverter" />
<f:validateLength minimum="1" maximum="18"></f:validateLength>
</h:inputText>
</h:panelGrid>
<h:commandButton id="reset2" type="submit" value="#{bundle.label}" onclick="resetField()"
styleClass="button" action="#{myBean.reset2}" immediate="true" />
</h:form>
<h:form id="form3" rendered="true">
<h:panelGrid columns="8">
<h:outputText value="#{bundle.label}" />
<h:inputText id="field3" size="18" maxlength="18" immediate="true"
value="#{myBean.myField3}">
<f:converter converterId="LongConverter" />
<f:validateLength minimum="1" maximum="18"></f:validateLength>
</h:inputText>
</h:panelGrid>
<h:commandButton id="reset3" type="submit" value="#{bundle.label}" onclick="resetField()"
styleClass="button" action="#{myBean.reset3}" immediate="true" />
</h:form>
<h:form id="form4" rendered="true">
<h:panelGrid columns="8">
<h:outputText value="#{bundle.label}" />
<h:inputText id="field4" size="18" maxlength="18" immediate="true"
value="#{myBean.myField4}">
<f:converter converterId="LongConverter" />
<f:validateLength minimum="1" maximum="18"></f:validateLength>
</h:inputText>
</h:panelGrid>
<h:commandButton id="reset4" type="submit" value="#{bundle.label}" onclick="resetField()"
styleClass="button" action="#{myBean.reset4}" immediate="true" />
</h:form>
我真的不知道问题出在按钮还是多个表单共享的 ajax 上?当上面有多个重置按钮/字段时,有人可以告诉我正确的方法吗?
编辑: 我尝试从“重置”按钮中删除所有 immediate=true,当然现在单击“重置”按钮时会显示错误消息。但我注意到它只在最后三个表单重置按钮上起作用,而不是在第一个......?
我希望这能让知道是什么原因的人有更深入的了解
【问题讨论】:
-
type="submit" 的目标是什么?
-
@Tires 我认为需要为 action="" 属性调用支持 bean myBean.methodName?如果我错了,请随时纠正我
-
我尝试从“重置”按钮中删除所有的 immediate=true,当然现在单击“重置”按钮时会显示错误消息。但我注意到它只在最后三个表单重置按钮上这样做,而不是在第一个......
-
理想情况下,您的重置按钮应该是
type="reset"。现在的方式,整个封闭表单仍然被提交和验证 -
@Kairan Guess type="..." 可能会以非 JSF 方式更改提交行为,这会导致麻烦。我建议要么使用纯 JSF 元素,要么使用纯 HTML 自我实现的生命周期。