如何仅在成功提交表单后显示对话框 [英] How to display dialog only on complete of a successful form submit
问题描述
我有一个包含 2 个必需输入字段和一个显示对话框的命令按钮的表单:
I have a form with 2 required input fields and a command button which shows a dialog:
<p:commandButton id="showDialogButton" value="Enregistrer"
action="#{DEQbean.Ajouter()}" update="@form"
oncomplete="dialogaboutDEQ.show()" />
<p:dialog id="reamrquesDEQ" widgetVar="dialogaboutDEQ" header="Informations"
width="400" closable="false" resizable="false" showEffect="clip"
hideEffect="clip" modal="true" appendToBody="true">
<p:messages id="messages" showDetail="true" autoUpdate="true"/>
<p:commandButton value="OK" action="DEQlist" />
</p:dialog>
我想仅在验证通过并执行操作方法时才显示对话框.但是,当所需的输入字段为空且未执行操作方法时,仍会显示对话框.
I would like to show the dialog only when the validation has passed and the action method is executed. However, when the required input fields are empty and the action method is thus not executed, the dialog is still shown.
如何仅在表单提交成功后才显示对话框?
How do I display the dialog only on complete of a successful form submit?
推荐答案
PrimeFaces ajax 响应将 args
对象放入具有 validationFailed
属性的 JS 范围内.您可以在 oncomplete
中检查.
The PrimeFaces ajax response puts an args
object in the JS scope which has a validationFailed
property. You could just check for that in the oncomplete
.
<p:commandButton ... oncomplete="if (args && !args.validationFailed) dialogaboutDEQ.show()" />
如果您在操作方法中执行 验证而不是在普通验证器中,并且您无法对其进行返工,那么您需要手动调用 FacesContext#validationFailed()
.
If you're performing validation in action method instead of in a normal validator, and you can't rework that, then you need to manually call FacesContext#validationFailed()
.
另一种选择是使用 RequestContext#execute()
内部操作方法以编程方式指示 PrimeFaces 执行给定的 JS 代码.因此,除了 oncomplete
,您还可以在操作方法中执行此操作:
A different alternative is to use RequestContext#execute()
inside action method to programmatically instruct PrimeFaces to execute the given piece of JS code. So, instead of the oncomplete
, you could also do this in action method:
RequestContext.getCurrentInstance().execute("dialogaboutDEQ.show()");
如果验证失败,则不调用 action 方法,也不会执行.
If the validation fails, then the action method is not invoked and then this would also not be executed.
另外一种替代方法是使用对话框的 visible
属性.您的命令按钮显然正在更新包括对话框在内的整个表单(即使对话框本身是用 appendToBody="true"
错误声明的,这将导致它不再位于父表单内;即它必须有 appendToBody="false"
或有自己的形式,但是 ala).你可以检查例如关于 FacesContext#isPostback()
和 FacesContext#isValidationFailed()
如果执行成功回发:
Again a different alternative is to use the dialog's visible
attribute. Your command button is apparently updating the whole form including the dialog (even though the dialog is at its own badly declared with appendToBody="true"
which would cause it to not be inside the parent form anymore; i.e. it must have appendToBody="false"
or have its own form, but ala). You could check e.g. on FacesContext#isPostback()
and FacesContext#isValidationFailed()
if a successful postback is performed:
<p:dialog ... visible="#{facesContext.postback and not facesContext.validationFailed}">
另见:
- < 的渲染属性和可见属性之间的区别p:对话框>
- 如何在 primefaces 中显示带有 requiredMessages 的弹出窗口,仅当这些消息存在时?
这篇关于如何仅在成功提交表单后显示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!