本文介绍了当发生验证错误后提交对话框:存放p的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小例子对话框:

<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>

我希望能够做的是有mbean.saveMethod莫名其妙prevent该对话框关闭,如果有一些问题,只能输出通过咆哮的消息。这是一种情况,即验证器也无济于事,因为没有办法判断,直到保存提交到后端服务器someValue中是有效的。目前,我这样做是使用可视属性,并将其指向一个布尔字段中的MBean。这一工程,但它使用户界面慢,因为突然出现向上或向下的对话框需要击中服务器。

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.

推荐答案

您可以删除的onSuccess 和PrimeFaces'代替它的RequestContext#执行()

You could remove the onsuccess and replace it by PrimeFaces' RequestContext#execute():

<p:commandButton value="Save"
    actionListener="#{mbean.saveMethod}" />

这个里面 saveMethod()

if (success) {
    RequestContext.getCurrentInstance().execute("testDialog.hide()");
}

如果您preFER不凌乱与视图特定的脚本控制器,可以作为选择可以考虑使用的onComplete ,而不是它提供了一个 ARGS 对象,它有一个布尔 validationFailed 属性:

If you prefer to not clutter the controller with view-specific scripts, you could alternatively also consider using oncomplete instead which offers an args object which has a boolean validationFailed property:

<p:commandButton value="Save"
    actionListener="#{mbean.saveMethod}"
    oncomplete="if (args &amp;&amp; !args.validationFailed) testDialog.hide()" />

(即如果(参数)检查是必要的,因为它可能不存在已经发生Ajax错误的时候,因此,当您试图造成一种新的JS错误GET validationFailed 从它; &放大器;放大器; 而不是&安培; 是强制性的this回答,重构如有需要,您调用像的onComplete =hideDialogOnSuccess(参数,testDialog)

(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 &amp; 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)")

这是不幸的PrimeFaces不支持什么样的RichFaces已经支持:请求时EL在上*复评属性。否则你将也能够做到这这一点:

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 value="Save"
    actionListener="#{mbean.saveMethod}"
    oncomplete="if (#{not facesContext.validationFailed}) testDialog.hide()" />

这篇关于当发生验证错误后提交对话框:存放p的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:12