在执行ajax命令时如何查找验证错误的指示(required ="true") [英] How to find indication of a Validation error (required="true") while doing ajax command

查看:90
本文介绍了在执行ajax命令时如何查找验证错误的指示(required ="true")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在对话框中有一个表单,可以通过单击带有ajax的commandbutton来关闭它,

I have a form inside a dialog which I close by clicking on commandbutton with ajax ,

像这样

<h:commandButton value="Add" action="#{myBean.addSomething(false)}"
    id="add_something_id" >
    <f:ajax render="@form someTable" execute="@form"
        onevent="closeAddNewSomethingDialogIfSucceeded"></f:ajax>
</h:commandButton>

这是关闭对话框的js代码

and here is the js code for closing the dialog

    function closeAddNewSomethingDialogIfSucceeded(data) {
        if(data.status === 'success') {
            $("#dialog_id").dialog("close");
        }
    }

直到这里都没有问题...

No problems till here...

现在我将某些对话框表单字段更改为required="true",现在我想防止由于验证错误而关闭对话框...

Now I changed some of the dialog form fields into required="true" and now I want to prevent the closing of the dialog of i got validation errors...

但是ajax data.status仍然达到其success状态,我无法弄清楚我可以挂在钩子上的验证失败的迹象...

But the ajax data.status still reaches its success state , and I can't figure out what indication of validation failure I can hook on...

有什么想法吗?

感谢BalusC回答,我做了以下事情:

在JSF中,添加了:

    <h:panelGroup id="global_flag_validation_failed_render">
        <h:outputText id="global_flag_validation_failed" value="true" 
            rendered="#{facesContext.validationFailed}"/>
    </h:panelGroup>

f:ajax更改为

<f:ajax render="@form someTable global_flag_validation_failed_render"

并且在js中添加了以下检查

and in js added the following check

if(data.status === 'success') {
    if($("#global_flag_validation_failed").length === 0){
         $("#dialog_id").dialog("close");
    }
}

推荐答案

不是专门针对required="true"的,但是您可以通过

Not specifically for required="true", but you can check by #{facesContext.validationFailed} if validation has failed in general. If you combine this with checking if the button in question is pressed by #{not empty param[buttonClientId]}, then you can put it together in the rendered attribute of the <h:outputScript> as follows:

<h:commandButton id="add_something_id" binding="#{add}" value="Add" action="#{myBean.addSomething(false)}">
    <f:ajax execute="@form" render="@form someTable" />
</h:commandButton>
<h:outputScript rendered="#{not empty param[add.clientId] and not facesContext.validationFailed}">
    $("#dialog_id").dialog("close");
</h:outputScript>

(请注意,您需要确保f:ajax也重新渲染了脚本)

有点骇人听闻,但由于标准JSF实现未在ajax响应中提供有关验证状态的任何信息,因此无法在onevent函数中进行处理.

A bit hacky, but it's not possible to handle it in the onevent function as the standard JSF implementation doesn't provide any information about the validation status in the ajax response.

如果碰巧使用RichFaces,则只需在<a4j:xxx>命令按钮/链接的oncomplete属性中使用EL.也就是说,它们是根据每个请求而不是标准JSF和PrimeFaces中的每个视图进行评估的.

If you happen to use RichFaces, then you could just use EL in the oncomplete attribute of the <a4j:xxx> command button/link. They are namely evaluated on a per-request basis instead of on a per-view basis as in standard JSF and PrimeFaces:

<a4j:commandButton ... oncomplete="if (#{!facesContext.validationFailed}) $('#dialog_id').dialog('close')" />

或者,如果您碰巧使用了PrimeFaces,那么您可以利用PrimeFaces通过附加的args.validationFailed属性扩展ajax响应这一事实,该属性直接注入oncomplete属性的JavaScript范围内:

Or if you happen to use PrimeFaces, then you could take advantage of the fact that PrimeFaces extends the ajax response with an additional args.validationFailed attribute which is injected straight in the JavaScript scope of the oncomplete attribute:

<p:commandButton ... oncomplete="if (args &amp;&amp; !args.validationFailed) $('#dialog_id').dialog('close')" />

(请注意,使用&amp;代替了&,因为&是XML/XHTML中的特殊字符)

(note that &amp; is been used instead of &, because & is a special character in XML/XHTML)

或者您可以在bean的action方法中使用PrimeFaces的RequestContext API在已渲染的视图中以编程方式执行JavaScript.

Or you could use the PrimeFaces' RequestContext API in the bean's action method to programmatically execute JavaScript in the rendered view.

RequestContext.getCurrentInstance().execute("$('#dialog_id').dialog('close')");

不需要任何条件检查,因为验证失败后,无论如何都不会调用bean的action方法.

No conditional checks are necessary as the bean's action method won't be invoked anyway when the validation has failed.

这篇关于在执行ajax命令时如何查找验证错误的指示(required ="true")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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