【问题标题】:Prevent confirmation dialogue from opening when there's a validation error出现验证错误时阻止打开确认对话框
【发布时间】:2013-03-17 01:26:15
【问题描述】:

我在手风琴面板的标签内有一个文本区域,它是描述。我正在尝试编辑描述并保存它。我正在验证文本区域,以便最大字符不应超过 1000 个字符。我正在使用<p:message> 来显示验证消息。在实际保存之前,将显示一个确认对话框以确认保存。

<p:messages showDetail="true" autoUpdate="true" />

<p:accordionPanel dynamic="true">
    <p:tab id="_0" title="description">
        <p:inputTextarea styleClass="max" id="editDesc1" widgetVar="txtBox" value="#{testBean.description}" 
            rows="6" cols="150" validatorMessage="#{msg.AddSystem_validationMsg5}" autoResize="false">
            <f:validateLength maximum="1000"></f:validateLength> 
        </p:inputTextarea>

        <p:commandButton value="save"  oncomplete="saveDialog.show()"/>

        <p:confirmDialog message="#{msg.EditSystem_confirmMsg1}" width="200" 
            showEffect="explode" hideEffect="explode" 
            header="Confirm" severity="alert" widgetVar="saveDialog"> 
            <p:commandButton value="#{msg.EditSystem_confirmAnswer1}"  action="#{testBean.saveEdit}" process="@this" /> 
            <p:commandButton value="#{msg.EditSystem_confirmAnswer2}" onclick="saveDialog.hide()" type="button" /> 

如果用户输入超过 1000 个字符并尝试保存,则验证消息会出现一小段时间,然后弹出确认对话框,导致验证消息消失。验证错误时如何防止弹出确认对话框?

【问题讨论】:

    标签: validation jsf-2 primefaces dialog messages


    【解决方案1】:

    如果验证没有失败,您需要签入保存按钮的oncomplete。 PrimeFaces 将一个全局 args 对象放在 JavaScript 范围内,该对象又具有一个布尔值 validationFailed 属性。你可以利用它:

    <p:commandButton value="save" oncomplete="if (args &amp;&amp; !args.validationFailed) saveDialog.show()"/>
    

    这样,只有在验证没有失败时才会显示确认对话框。

    【讨论】:

      【解决方案2】:

      我认为你可以使用 javascript 来验证:

      <script type="text/javascript">
      function test(){
      // validation here
      if(isValidated){saveDialog.show()}
      else alert('exceed ...');
      }
      </script>
      <p:commandButton value="save"  onclick="test()"/>
      

      【讨论】:

      • 这完全违背了服务器端验证的目的和能力。绝对不推荐这种方法。
      • 是的,我忘记了 args.validationFailed,所以我建议这样做,谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      相关资源
      最近更新 更多