2011-02-16 27 views
0

我在我的應用程序中使用了icefaces。 頁面中有一個表單,它有幾個輸入字段。只有在所有其他組件都無錯時啓用JSF commandbutton

我想使命令按鈕只有在字段有效(對其他組件沒有錯誤)

是否有可能做到這一點?

示例代碼

<ice:form> 
<ice:panelGrid columns="2"> 
    <ice:outputLabel>Enter Date</ice:outputLabel> 
     <ice:panelGroup> 
      <ice:selectInputDate renderAsPopup="true" id="InputDate" value="#{Bean.FormDate}" partialSubmit="true" ></ice:selectInputDate> 
       <ice:message for="InputDate" /> 
     </ice:panelGroup> 

    <ice:outputLabel>Days</ice:outputLabel> 
     <ice:panelGroup> 
      <ice:inputText value="#{Bean.days}"partialSubmit="true" id="inputDays"> 
       <f:validateLongRange minimum="1" maximum="10"/> 
      </ice:inputText> 
       <ice:message for="inputDays"/> 
      </ice:panelGroup> 

    <ice:commandButton value="Submit" action="#{Bean.SomeAction}"></ice:commandButton> 
</ice:panelGrid> 

在上面的代碼,我想啓用提交命令按鈕只有在天,日期是有效的(不要有任何錯誤排隊。

回答

1

如果使用的是JSF 2.0,你可以做這樣的事情:

<ice:form> 
     <ice:panelGrid columns="2"> 
      <ice:outputLabel>Enter Date</ice:outputLabel> 
      <ice:panelGroup> 
       <ice:selectInputDate renderAsPopup="true" id="InputDate" 
        value="#{Bean.FormDate}" partialSubmit="true" 
        binding="#{inputDateComponent}"> 
        <f:ajax execute="@this" render="submit inputDateMessage" /> 
       </ice:selectInputDate> 
       <ice:message id="inputDateMessage" for="InputDate" /> 
      </ice:panelGroup> 

      <ice:outputLabel>Days</ice:outputLabel> 
      <ice:panelGroup> 
       <ice:inputText value="#{Bean.days}" partialSubmit="true" 
        id="inputDays" binding="#{inputDaysComponent}"> 
        <f:validateLongRange minimum="1" maximum="10" /> 
        <f:ajax execute="@this" render="submit inputDaysMessage" /> 
       </ice:inputText> 
       <ice:message id="inputDaysMessage" for="inputDays" /> 
      </ice:panelGroup> 

      <ice:commandButton id="submit" value="Submit" 
       action="#{Bean.SomeAction}" 
       disabled="#{!inputDateComponent.valid}" /> 
     </ice:panelGrid> 
    </ice:form> 

我的天堂沒有測試過這個具體的例子,但你可以看到我要去哪裏。輸入字段會在更改時進行評估,並顯示驗證錯誤或創建未禁用commandButton的條件。

如果您希望按鈕僅在輸入字段有效時才顯示,您也可以使用rendered =並進行肯定的斷言。

+0

我明白你要去哪裏,但你知道IceFaces已經內置ajax支持嗎? – BalusC 2011-02-17 03:10:59

+0

我以前從來沒有用過icefaces。 f:ajax可以被替換爲冰面ajax函數。只是想說明這個概念。 – 2011-02-17 14:38:35

0

這裏描述你可以使用一個階段監聽器:

http://balusc.blogspot.com/2007/12/set-focus-in-jsf.html

的階段監聽器在這個例子中建立與具有附加信息客戶ID字符串。在一個bean中檢查這個字符串,並根據是否有帶有消息的id來創建一個bean屬性。

然後,您可以啓用命令按鈕,具體取決於bean屬性。

0

將您的每個輸入字段切換到'immediate =「true」'並向字段添加驗證器。

讓驗證器設置/取消設置布爾值,以確定這些字段是否包含有效數據,並按照sparrowhawk的建議在提交按鈕的渲染表達式中測試此布爾值。

相關問題