2016-07-25 91 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,但我更新了我的答案。我开箱工作。只需复制粘贴即可。 –