问题描述
我在 PrimeFaces 4.0 中使用 JSF 2.
I'm using JSF 2 with PrimeFaces 4.0.
我有一个包含一些输入和验证的表单.从对话框中选择实体时,某些字段可能会自动填充.当我在提交整个表单(并验证)之前选择实体时,这很好用.但是当我在验证后选择一个实体时,组件的更新不起作用.
I have a form with some inputs and validation. Some fields may automatically filled when choosing an entity from a dialog. This works fine when I choose the entity before I submit the whole form (and validate). But when I choose an entity after validation the update of the component does not work.
JSF 页面:
<h:form id="form">
<p:inputText id="id" value="#{bean.user.id}" required="true"
onclick="PF('usersDialog').show()" />
<p:inputText id="name" value="#{bean.user.name}" required="true"
onclick="PF('usersDialog').show()" />
<p:commandButton value="submit" action="#{bean.submit}" update=":form" />
</h:form>
<p:dialog widgetVar="usersDialog">
<h:form>
<p:dataTable value="#{bean.users}" var="user">
<p:column>
<p:commandButton value="choose" onclick="PF('usersDialog').hide()"
process="@this" action="#{bean.select(user)}" update=":form" />
</p:column>
<p:column>
<h:outputText value="#{user.name}" />
</p:column>
</p:dataTable>
</h:form>
</p:dialog>
我知道这取决于 JSF 生命周期,但我无法修复它.那么,从对话框中选择新实体时,如何在验证失败后更新表单?
I know its up to the JSF lifecycle, but I'm not able to fix it.So how can I update the form after validation fail when choosing an new entity from dialog?
提前致谢.
推荐答案
正是为了这个目的, 组件 被引入(基于 OmniFaces
ResetInputAjaxActionListener
).
Exactly for this purpose, <p:resetInput>
component was introduced (based on OmniFaces ResetInputAjaxActionListener
).
将其嵌套在命令按钮内,将 target
设置为与命令按钮的 update
属性相同的客户端 ID:
Nest it inside the command button, with a target
set to the same client ID as update
attribute of command button:
<p:commandButton ... update=":form">
<p:resetInput target=":form" />
</p:commandButton>
另见:
- 如何在发生验证错误后使用 PrimeFaces AJAX 填充文本字段?
- 重置输入字段而不执行验证
这篇关于使用新值更新表单时,如何在验证失败后重置表单中的输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!