在不执行验证的情况下重置输入字段 [英] Reset input fields without executing validation

查看:27
本文介绍了在不执行验证的情况下重置输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Facelets 视图,如下所示:

I have a Facelets view as below:

<h:form id="f1">
<p:panelGrid id="p1" columns="2"> 
<p: inputText value="Distance Travelled::/><p:inputText value="#{airTransportUsage.distance}" immediate="true"
required="true" requiredMessage="Distance Travelled Field cannot be left blank.."
converterMessage="Distance Travelled must be a number"
validatorMessage="Distance Travelled must be a valid number.."
id="dis">
<f:validateLongRange minimum="1"/>
</p:inputText>
<p:commandButton value="Reset" action="#{airTransportUsage.reset}" update=":f1:p1" />
</p:panelGrid>
</h:form>

当reset按钮被点击时,相应的方法由于验证永远无法执行.我无法在我的重置按钮上使用 immediate="true",因为它会产生一些其他问题.

When the reset button is clicked, the corresponding method can never be executed due to validation. I can't use immediate="true" on my reset button as it creates some other problems.

推荐答案

确实默认处理整个表单 (process="@form"code>),您可以通过在 process 属性中仅指定当前组件来更改此设置.

The <p:commandButton> processes indeed by default the entire form (process="@form"), you can change this by specifying only the current component in the process attribute.

<p:commandButton value="Reset" ... process="@this" />

但是,如果表单已经预先验证过,这将失败.已标记为无效的输入字段不会使用新模型值(您自己重置)进行更新.如果您使用的是 PrimeFaces 3.4,则嵌入 <p:resetInput> 在按钮中:

However, this will fail if the form is already been validated beforehand. The input fields which have been marked invalid won't be updated with the new model value (which you've resetted yourself). If you're using PrimeFaces 3.4, then embed <p:resetInput> in the button:

<p:commandButton value="Reset" ... process="@this">
    <p:resetInput target="@form" />
</p:commandButton>

如果您还没有使用 PrimeFaces 3.4 并且无法升级到它,您可以使用 OmniFaces ResetInputAjaxActionListener为此.

If you aren't on PrimeFaces 3.4 yet and can't upgrade to it, you can use OmniFaces ResetInputAjaxActionListener for this.

另一种完全不同的选择是通过新的 GET 请求刷新当前页面.

A completely different alternative is to just refresh the current page by a fresh new GET request.

<p:button value="Reset" />

这篇关于在不执行验证的情况下重置输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆