【问题标题】:Primefaces: required field message overrides <p:dialog/> messagePrimefaces:必填字段消息覆盖 <p:dialog/> 消息
【发布时间】:2013-02-06 19:14:36
【问题描述】:

我有一个具有必需属性的字段。当我按下 Accept 按钮保存一些数据而不在字段中输入任何值时,会显示一条错误消息。到现在为止还挺好。 但是,如果在那之后我决定单击 Cancel 按钮,则该错误消息会覆盖应该显示在 &lt;p:dialog/&gt; 元素内的确认消息。

注意:如果我改用&lt;p:confirmDialog/&gt; 组件,似乎没有问题,因为我猜它使用message="" 属性,没有&lt;p:messages/&gt; 标记。

XHTML

<p:dialog>
<p:outputPanel>
      <h:form>

      <h:outputText value="Field:"/>
      <p:inputText id="field" value="" type="text" required="true" requiredMessage="You must complete the field" />                         
      <p:growl id="messages" showDetail="true"/>

      <p:commandButton id="dialogCancel" value="Cancel" oncomplete="confirmCancelDialog.show();"  actionListener="#{controller.addCloseWarn}" />    

      </h:form>
</p:outputPanel>
</p:dialog>

<h:form>
<p:dialog id="confirmCancelDialog" header="Warning!" widgetVar="confirmCancelDialog" modal="true" >                                         
    <p:messages id="closeMessage" showDetail="true" autoUpdate="true" />                                    
    <p:commandButton id="confirm" value="Accept" onclick="..." />  
    <p:commandButton id="decline" value="Cancel" onclick="..." />               
</p:dialog> 
</h:form>

Bean 控制器

public void addCloseWarn(ActionEvent actionEvent) { 
        FacesContext.getCurrentInstance().addMessage("closeMessage", new FacesMessage(FacesMessage.SEVERITY_WARN, null, 
                          "Are you sure you want to leave the page?")); 
}

【问题讨论】:

  • 你能发布 xhtml 页面的结构,其中表单的位置是输入字段和按钮吗?这个带有对话框的表单是否可能在另一个表单中?
  • @partlov 当然。在那里我编辑了帖子。带有对话框的表单与包含输入字段的表单是分开的。

标签: jsf dialog primefaces validation


【解决方案1】:

取消按钮的问题是您的表单已提交并执行了验证。你可以给commandButton添加process="@this"属性,这样表单的其他部分就不会被处理,而你的addCloseWarn方法会被执行。

我还要补充一点,这可能不是message 标签的标准用法。它用于显示错误、警告和成功消息,而不是确认问题。因此,请使用confirmDialog 或使用标准对话框,仅包含普通文本和 OK - Cancel 按钮。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 2012-12-18
    相关资源
    最近更新 更多