如何在 PrimeFaces oncomplete 中触发 args.validationFailed [英] How to trigger args.validationFailed in PrimeFaces oncomplete

查看:32
本文介绍了如何在 PrimeFaces oncomplete 中触发 args.validationFailed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当按下 Save 按钮时,应该验证 actionsDialog 中的数据.如果输入的所需信息有效,则会显示第二个名为 reasonDialog 的对话框.

When the Save button is pressed, the data from the actionsDialog should be validated. If the required information is entered and valid, a second dialog called reasonDialog would be displayed.

已保存对象的非 JSF 验证是通过返回错误消息列表的方法完成的.如果验证失败,则使用 FacesMessage 显示错误消息.我应该如何在 objectsBean.validate 中触发 oncomplete 中的 if else 子句?

Non-JSF validation of the saved object is done with a method that returns a list of error messages. I case the validation fails the errors messages are displayed with FacesMessage. How should I do in objectsBean.validate to trigger the if else clause from oncomplete?

<p:dialog id="actionsDialog" widgetVar="actionsDialog" dynamic="true" 
    resizable="false" width="800" modal="true">
    <ui:include src="/WEB-INF/flows/custom-flow/genericObject.xhtml"/>
    <f:facet name="footer">
        <p:commandButton value="Save" update="msgs"
            oncomplete="if (args.validationFailed) {reasonDialog.hide()} else {reasonDialog.show()}"
            actionListener="#{objectsBean.validate}"/>
        <p:commandButton value="Cancel" immediate="true" oncomplete="actionsDialog.hide()" />
    </f:facet>
</p:dialog>

推荐答案

如果使用 JSF 内置的验证工具(即只使用抛出 ValidatorException 的验证器,其中包含所需的面部消息的通常方式)出于某种原因,这真的不是一个选择(我真的会三思而后行,不,围绕 JSF 验证工具工作三次),那么您始终可以使用 FacesContext#validationFailed() 通知 JSF 验证失败一般,这正是 JSF 验证工具在捕获 ValidatorException 时在幕后所做的事情.

If utilizing the JSF-builtin validation facility (i.e. just use validators which throw ValidatorException the usual way with therein the desired faces message) is really not an option for some reason (I'd really think twice, no, thrice about working around JSF validation facility), then you can always use FacesContext#validationFailed() to signal JSF that the validation has failed in general, which is exactly what JSF validation facility is doing under the covers when a ValidatorException is caught.

FacesContext.getCurrentInstance().validationFailed();

这篇关于如何在 PrimeFaces oncomplete 中触发 args.validationFailed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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