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

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

问题描述

我在对话框中有一个表单,我通过单击带有 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代码

 函数 closeAddNewSomethingDialogIfSucceeded(data) {if(data.status === '成功') {$("#dialog_id").dialog("close");}}

到这里都没问题...

现在我将一些对话框表单字段更改为 required="true" 并且现在我想防止在出现验证错误时关闭对话框...

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

有什么想法吗?

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

在 JSF 中,添加:

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

将 f:ajax 改为

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

并在js中添加了以下检查

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

解决方案

不是专门针对 required="true",但可以通过 #{facesContext.validationFailed} 如果验证一般都失败了.如果您将此与检查有问题的按钮是否被 #{not empty param[buttonClientId]},然后就可以放在rendered属性中="http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/h/outputScript.html" rel="noreferrer"> 如下:

<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 render="#{not empty param[add.clientId] and not facesContext.validationFailed}">$("#dialog_id").dialog("close");</h:outputScript>

(注意你需要确保脚本也被f:ajax重新渲染了)

有点笨拙,但不可能在 oneevent 函数中处理它,因为标准 JSF 实现不提供任何有关 ajax 响应中验证状态的信息.

如果您碰巧使用 RichFaces,那么您可以在 命令按钮/链接的 oncomplete 属性中使用 EL.它们是基于每个请求而不是像标准 JSF 和 PrimeFaces 那样基于每个视图进行评估的:

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

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

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

(注意,使用了 &amp; 而不是 &,因为 & 在XML/XHTML)

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

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

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

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

like this

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

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

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

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

any ideas?

Thanks to BalusC answer I did the following:

in JSF , added :

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

the f:ajax was changed into

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

and in js added the following check

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

解决方案

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>

(note that you need to make sure that the script is also re-rendered by f:ajax)

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.

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')" />

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')" />

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

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')");

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

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

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