2016-07-25 89 views
0

我希望我的面板保持可見狀態,當我點擊命令按鈕並且執行的方法調用錯誤消息時。防止rich:popupPanel點擊命令按鈕後關閉

更具體地說,我有一個輸入字段的驗證器,它應該會得到一個日期。如果這個日期無效,我的支持bean中的validate方法會創建一條錯誤消息。這應該顯示在使用命令按鈕後彈出面板中的輸入字段旁邊。

點擊命令按鈕關閉彈出窗口。但是如果我重新打開它,會顯示錯誤消息,這讓我不知道爲什麼它在第一時間關閉,而最大嚴重性條件未得到滿足。

我的XHTML頁面:在支持Bean

<h:body> 
    <h:commandButton id="note" value="Neuer Satz"> 
     <rich:componentControl target="note_panel" operation="show" /> 
    </h:commandButton> 

    <rich:popupPanel id="note_panel" modal="true" autosized="true" 
     resizeable="false" header="Neuen Mehrwertsteuersatz vormerken" 
     domElementAttachment="form"> 
       Gültig ab 
       <h:inputText id="newVorGueltigAb" 
      value="#{mehrwertsteuerBean.neuGueltigAb}" maxlength="10" 
      validator="#{mehrwertsteuerBean.validateNewDate}" /> 
     <h:message for="newVorGueltigAb" style="color:red" /> 
     <h:commandButton value="Vormerken" 
      action="#{mehrwertsteuerBean.addSteuersatz()}" 
      oncomplete="if (#{facesContext.maximumSeverity==null}) 
        #{rich:component('note_panel')}.hide(); return false;" /> 
     <h:commandButton value="Abbrechen" 
      onclick="#{rich:component('note_panel')}.hide(); return false;" /> 
    </rich:popupPanel> 
</h:body> 

我的驗證方法:

所有的
public void validateNewDate(FacesContext context, UIComponent toValidate, 
      Object value) { 
     String regex = "([0-9]{2}).([0-9]{2}).([0-9]{4})"; 
     String date = (String) value; 
     if (date.matches(regex)) { 
      validNewDate = true; 
     } else { 
      validNewDate = false; 
      String message = "Bitte gültiges Datum eingeben!"; 
      context.addMessage(toValidate.getClientId(context), 
        new FacesMessage(message)); 

     } 
} 
+0

h:commandButton沒有'oncomplete'屬性。 – Makhiel

+0

噢,好的,謝謝! 但是,如果我使用a4j:commandButton出現另一個問題: 如果我嘗試提交錯誤的日期彈出不會按要求關閉,但不會顯示錯誤消息。 – Marco

+0

你必須渲染消息的地方。 BTW。只有在驗證newVorGueltigAb失敗時纔會顯示。因此,請查看整個表單的'h:messages'或'rich:messages'。 BTW2。如果你想驗證失敗,你必須拋出'ValidatorException'。 –

回答

0

首先,當你想驗證失敗,你必須拋出異常。所以它必須看起來是這樣的:

public void validateNewDate(FacesContext context, UIComponent toValidate, Object value) { 
    String regex = "([0-9]{2}).([0-9]{2}).([0-9]{4})"; 
    String date = (String) value; 
    if (!date.matches(regex)) { 
     String message = "Bitte gültiges Datum eingeben!"; 
     throw new ValidatorException(
      new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null)); 
    } 
} 

如果這樣做會有模型,沒有發生在你的commandButton不會被解僱的動作。這是JSF驗證的正確流程。

第二件事。你必須手動處理驗證錯誤。要檢查是否有一些錯誤,我用這個:

public boolean isNoErrorsOccured() { 
    FacesContext facesContext = FacesContext.getCurrentInstance(); 
    return ((facesContext.getMaximumSeverity() == null) || 
       (facesContext.getMaximumSeverity() 
        .compareTo(FacesMessage.SEVERITY_INFO) <= 0)); 
} 

而與此您可以a4j:commandButton是這樣的:

<a4j:commandButton value="Vormerken" execute="note_panel" 
    action="#{mehrwertsteuerBean.addSteuersatz}" 
    render="note_panel" 
    oncomplete="if (#{facesHelper.noErrorsOccured}) {#{rich:component('note_panel')}.hide();} 
       else { console.log('OMG! I\'ve got an error!'); }" /> 

更新:

都應該看起來像這樣:

<rich:popupPanel id="note_panel" modal="true" autosized="true" resizeable="false" 
    header="Neuen Mehrwertsteuersatz vormerken" domElementAttachment="body"> 
    Gültig ab 
    <h:form> 
     <h:inputText id="newVorGueltigAb" value="#{mehrwertsteuerBean.neuGueltigAb}" maxlength="10" 
      validator="#{mehrwertsteuerBean.validateNewDate}" /> 
     <h:message for="newVorGueltigAb" style="color:red" /> 
     <a4j:commandButton value="Vormerken" execute="@form" action="#{mehrwertsteuerBean.addSteuersatz}" 
      render="@form" 
      oncomplete="if (#{mehrwertsteuerBean.noErrorsOccured}) {#{rich:component('note_panel')}.hide();}" /> 
     <h:commandButton value="Abbrechen" 
      onclick="#{rich:component('note_panel')}.hide(); return false;" /> 
    </h:form> 
</rich:popupPanel> 

注:

  • 缺少<h:form>
  • render屬性,因爲在驗證錯誤的情況下必須渲染面板。
  • action末尾沒有括號。
  • execute屬性設置爲僅彈出。這將導致整個表單中的其他字段不會更新。
  • 由於h:form在彈出框內,我已將domElementAttachment更改爲正文。
+0

謝謝你的回答Emil,但它仍然不適用於你的代碼。起初,我只是複製粘貼到我的頁面,導致popupPanel不關閉,即使我輸入了正確的日期。此外,錯誤未顯示。如果我用另一個按鈕「Abbrechen」關閉面板並重新打開,則顯示消息。 然後我刪除了一些屬性,並試圖解決問題,似乎執行和渲染屬性一起阻止面板關閉即使日期有效。 爲什麼消息不能立即顯示,我不能說。 – Marco

+0

此外,我發現,當點擊「Vormerken」按鈕時,控制檯顯示以下錯誤: TypeError:RichFaces。$(...)未定義 – Marco

+0

我通常不這樣做,因爲它看起來像你必須先學習JSF,但我更新了我的答案。我開箱工作。只需複製粘貼即可。 –