2012-11-01 19 views
1

我是JSF的新手,我遇到了一個奇怪的問題,不知道如何解決它。jsf在文本字段驗證期間的標籤導航

我有多個選項卡中的畫面,並在標籤中的一個,我有文本框,下拉菜單等 所有的文本字段和下拉菜單都需要=「真」集。

由於此驗證,當我嘗試導航到另一個選項卡而沒有輸入或選擇值時,我無法導航並顯示驗證消息(值爲必填項)。

但我有一個保存按鈕在選項卡中,驗證應該只有當我點擊保存按鈕,而不是在Tab切換/導航期間觸發。在驗證工作之間,當我點擊保存按鈕時,我所需要的是當切換到不同的選項卡時不應觸發驗證。

此驗證不允許我切換到不同的選項卡,而不是 確定爲什麼在切換選項卡時會觸發此驗證。

請問任何人都可以爲我的問題提出解決方案。


更新:這裏是我做了基於CycDemo的答案的變化:

<h:tab name="First Tab" accessKey="s"> 
    <h:panelGroup > 
    <h:dataTable id="firstTable" styleClass="dataTableEx" headerClass="headerClass" 
       footerClass="footerClass" rowClasses="rowClass1,rowClass2" 
       columnClasses="columnClass1" value="#{clientProductCRUDBean.model}" 
       var="item" border="1" cellpadding="2" cellspacing="0" > 

       <!-- this for the incoming originator type dropdown field --> 

       <h:column> 
        <f:facet name="header"> 
         <h:outputText value="firstDropDown" /> 
        </f:facet> 
        <h:selectOneMenu id="firstDropDown" 
           required="true" requiredMessage="required field" immediate="true" > 
          <f:selectItem itemLabel="--Select--" itemValue="#{null}" /> 
          <h:message for="firstDropDown" errorClass="generalErrorText"/> 
        </h:selectOneMenu> 
       </h:column>         


       <!-- this for the outgoing originator text field --> 

       <h:column > 
        <f:facet name="header"> 
         <h:outputText value="firstTextField"/> 
        </f:facet> 
        <h:inputText id="firstTextField" 
        required="true" validatorMessage="Max Length 20" requiredMessage="required field" immediate="true" > 
        <f:validateLength maximum="20"/> 
        <h:message for="firstTextField" errorClass="generalErrorText"/> 
        </h:inputText> 
       </h:column> 

      <!-- this for the replace or randomise radio buttons text field --> 

        <!-- this for the operators selection drop down--> 

       </h:dataTable> 
    </h:panelGroup> 

    <h:panelGroup style="padding: 1px; margin-top: 5px;" > 
       <h:commandButton id="firstSaveButton" styleClass="commandButton" value="Save" /> 
       <h:commandButton id="firstSaveCancel" styleClass="commandButton" value="Cancel" immediate="true"/> 
    </h:panelGroup> 
    <br /> 
</h:tab> 

它仍然調用驗證。

+0

您可以發佈的代碼? – Swarne27

+0

對不起,我爲一家公司工作,我不能發佈代碼,但我可以保證當我從文本字段中刪除required =「true」並下拉菜單時,我可以導航到其他選項卡。可能是我可以嘗試想出一個簡單的代碼來複制這個問題。感謝您表現出興趣。 – likeToCode

回答

3

如果您想在沒有驗證過程的情況下使用switch/navigation,則必須在您的操作組件中使用immediate="true"

比方說,你有如下。

<h:commandButton value="Save" /> 
<h:commandButton value="NextPage" immediate="true"/> 

當您單擊Save按鈕,驗證過程將是工作。但是,點擊了NextPage驗證過程將被克服。

注意 如果有immediate="true"輸入組件,即使你單擊NextPage按鈕,驗證過程將被該組件執行。

<h:inputText id="name" value="#{...}" immediate="true" required="true"> 
<h:commandButton value="Save" /> 
<h:commandButton value="NextPage" immediate="true"/> 

如果單擊NextPage按鈕的<h:inputText id="name"...>驗證過程會執行。你可能會有一個問題。爲什麼他們這樣做? 答案是你必須學習JSF Life Cycle

+0

謝謝,不知道我是否理解了所有內容,但是我粘貼了來自您建議的更改的代碼,但是我仍然無法導航到其他選項卡,而無需在文本字段中輸入值或從下拉列表中選擇值。 – likeToCode

+0

您應該**不**已經將'immediate =「true」'添加到輸入組件中。另請參閱答案的「註釋」。 – BalusC

+0

@BalusC,老實說我在找你來救我,看過很多你的帖子,謝謝。對不起,這是我在輸入組件中的內容。 likeToCode

0

使用<rich:tabPanel並將switchType更改爲客戶端。然後在裏面使用<rich:tab>

<rich:tabPanel switchType="client">
            <rich:tab>
                        .............
            </rich:tab>
</rich:tabPanel>