【问题标题】:How to check if RichFaces component rich:messages has messages in Javascript?如何检查 RichFaces 组件 Rich:messages 是否有 Javascript 中的消息?
【发布时间】:2012-07-04 06:08:38
【问题描述】:

我需要检查渲染的 rich:messages 组件是否有任何来自 JavaScript 的消息。

在执行 AJAX 调用后,我正在通过 a4j:jsFunction 重新呈现 rich:messages 组件,并且我想检查 Javascript 是否附加了任何消息(这意味着验证未通过)。

a4j:jsFunction 的代码如下:

<a4j:jsFunction name="validateSomething" render="erroFinaliza"
        execute="@form" action="#{someMB.preview}">
</a4j:jsFunction>

现在,我在 h:commandButton 上调用它,如下所示:

<h:commandButton onclick="validateSomething(); return false;" value="Do Something" />

我需要一种通过 JavaScript 检查验证结果的方法,然后真正提交表单。

我检查了rich:messages docs,看看是否有办法检查它是否附加了任何消息,但它没有 Javascript API。

有人有更好的主意吗?

【问题讨论】:

    标签: javascript jsf-2 richfaces


    【解决方案1】:

    我在 this question 上找到了一个 hacky 解决方案。

    我将a4j:jsFunction 替换为仅用于执行验证的a4j:commandButton

    然后在这个a4j:commandButtononcomplete 上,我放了一个JS 来检查验证是否通过,然后“点击”另一个不可见的h:commandButton,它提交了真实的表单,调用了真实的动作。

    所以,它是这样的:

    <h:form id="id_form">
        <rich:messages id="errors" />
    
        ...
    
        <script>
        function submitIfEmpty(noMessages){
            if (noMessages){
                document.getElementById('id_form:hiddenButton').click();
            }
        };
        </script>
    
        ...
    
        <a4j:commandButton value="Do Something" render="errors" execute="@form"
             oncomplete="submitIfEmpty(#{empty facesContext.maximumSeverity});" />
        <h:commandButton id="hiddenButton" action="#{someMB.doSomething}"
             value="Do Something (not visible)" style="visibility: hidden" />
    </h:form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-13
      • 2017-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 2021-09-26
      相关资源
      最近更新 更多