提交后发生验证错误时,保持p:对话框打开 [英] Keep p:dialog open when a validation error occurs after submit
问题描述
最小示例对话框
< p:dialog header =Test Dialog
widgetVar =testDialog>
< h:form>
< p:inputText value =#{mbean.someValue}/>
< p:commandButton value =保存
onsuccess =testDialog.hide()
actionListener =#{mbean.saveMethod}/>
< / h:form>
< / p:dialog>
我想要做的是让mbean.saveMethod以某种方式阻止对话框关闭if有一些问题,只通过咆哮输出一个消息。这是一个验证器无法帮助的情况,因为在保存提交给后端服务器之前,无法确定someValue是否有效。目前,我使用visible属性,并将其指向mbean中的一个布尔字段。这样做有效,但是使用户界面变慢,因为弹出或关闭对话框需要击中服务器。
<$ c如果ajax请求本身成功(即没有网络错误,未捕获的异常等),则$ c> onsuccess 运行,而不是如果action方法被成功调用。
给定一个< p:dialog widgetVar =testDialog>
您可以删除 onsuccess
并将其替换为PrimeFaces RequestContext#execute()
in saveMethod()
:$($)
if(success){
RequestContext.getCurrentInstance()执行(PF('testDialog')。隐藏());
}
注意: PF()
在PrimeFaces 4.0中引入。在较旧的PrimeFaces版本中,您需要 testDialog.hide()
。
如果您不想混乱控制器使用视图特定的脚本,您可以使用 oncomplete
,而不是提供一个 args
对象,该对象具有布尔值 validationFailed
属性:
< p:commandButton .. 。
oncomplete =if(args& amp; amp;!args.validationFailed)PF('testDialog')。hide()/>
if(args)
因为当ajax错误发生时可能不存在,因此当您尝试从中获取 validationFailed
时会导致新的JS错误; & amp; amp;
而不是&
是必须的,因为这个答案,如果需要JS函数重构您调用的内容如 oncomplete =hideDialogOnSuccess(args,testDialog)
。
不幸的是,PrimeFaces不支持RichFaces已经支持的内容:请求时间重新评估
中的
属性。否则你也可以这么做: < p:commandButton ...
oncomplete =if(#{not facesContext.validationFailed})PF('testDialog')。hide()/>
Minimal example dialog:
<p:dialog header="Test Dialog"
widgetVar="testDialog">
<h:form>
<p:inputText value="#{mbean.someValue}"/>
<p:commandButton value="Save"
onsuccess="testDialog.hide()"
actionListener="#{mbean.saveMethod}"/>
</h:form>
</p:dialog>
What I want to be able to do is have the mbean.saveMethod somehow prevent the dialog from closing if there was some problem and only output a message through growl. This is a case where a validator won't help because there's no way to tell if someValue is valid until a save is submitted to a back end server. Currently I do this using the visible attribute and point it to a boolean field in mbean. That works but it makes the user interface slower because popping up or down the dialog requires hitting the server.
The onsuccess
runs if ajax request itself was successful (i.e. there's no network error, uncaught exception, etc), not if action method was successfully invoked.
Given a <p:dialog widgetVar="testDialog">
You could remove the onsuccess
and replace it by PrimeFaces RequestContext#execute()
inside saveMethod()
:
if (success) {
RequestContext.getCurrentInstance().execute("PF('testDialog').hide()");
}
Note: PF()
was introduced in PrimeFaces 4.0. In older PrimeFaces versions, you need testDialog.hide()
instead.
If you prefer to not clutter the controller with view-specific scripts, you could use oncomplete
instead which offers an args
object which has a boolean validationFailed
property:
<p:commandButton ...
oncomplete="if (args && !args.validationFailed) PF('testDialog').hide()" />
The if (args)
check is necessary because it may be absent when an ajax error has occurred and thus cause a new JS error when you try to get validationFailed
from it; the &
instead of &
is mandatory for the reason explained in this answer, refactor if necessary to a JS function which you invoke like oncomplete="hideDialogOnSuccess(args, testDialog)"
.
It's unfortunate that PrimeFaces does not support what RichFaces already supports: request-time re-evaluation of EL in on*
attributes. You would otherwise also be able to do just this:
<p:commandButton ...
oncomplete="if (#{not facesContext.validationFailed}) PF('testDialog').hide()" />
这篇关于提交后发生验证错误时,保持p:对话框打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!