2012-04-09 258 views
0

我有一個表格,我有一個rich:select,無論他們選擇選項A還是選項B,都會呈現h:inputText。 最初,對應於選項A的h:inputText已呈現,如果我將其留空,則會出現requiredMessage並且一切正常。 現在,當我選擇rich:select的第二個選項時,會顯示選項B的h:inputText,但如果我將其留空,則不會顯示requiredMessage渲染驗證消息RichFaces

我錯過了什麼?

我的XHTML代碼如下:

   <h:panelGrid columns="3"> 
        <h:outputLabel for="tipoPersona">Tipo de persona</h:outputLabel> 
        <rich:select id="tipoPersona" 
           required="true" value="#{userInformationController.tipoPersona}" 
           requiredMessage="Seleccione el tipo de persona" 
           defaultLabel="Seleccione una opción"> 
         <f:selectItems value="#{userInformationController.tipoPersonaOpciones}"/> 
         <f:ajax event="blur" render="tipoPersonaMessage"/> 
         <f:ajax event="selectitem" 
           render="outputLabelCURP inputTextCURP messageCURP outputLabelRFC inputTextRFC messageRFC" 
           listener="#{userInformationController.doRenderCURP}"/> 
        </rich:select> 
        <rich:message id="tipoPersonaMessage" ajaxRendered="true" 
            for="tipoPersona"/> 


        <a4j:outputPanel id="outputLabelCURP"> 
         <h:outputLabel for="CURP" value="CURP" 
             rendered="#{userInformationController.curpRendered}"/> 
        </a4j:outputPanel>       
        <a4j:outputPanel id="inputTextCURP"> 
         <h:inputText id="CURP" required="true" 
            requiredMessage="Introduzca el CURP" 
            rendered="#{userInformationController.curpRendered}"> 
          <f:ajax event="blur" render="curpMessage"/> 
         </h:inputText> 
        </a4j:outputPanel> 
        <a4j:outputPanel id="messageCURP"> 
         <rich:message id="curpMessage" ajaxRendered="true" 
             for="CURP" 
             rendered="#{userInformationController.curpRendered}"/> 
        </a4j:outputPanel> 


        <a4j:outputPanel id="outputLabelRFC"> 
         <h:outputLabel for="RFC" value="RFC" 
             rendered="#{not userInformationController.curpRendered}"/> 
        </a4j:outputPanel> 
        <a4j:outputPanel id="inputTextRFC"> 
         <h:inputText id="RFC" required="true" 
            requiredMessage="Introduzca el RFC" 
            rendered="#{not userInformationController.curpRendered}"> 
          <f:ajax event="blur" render="rfcMessage"/> 
         </h:inputText> 
        </a4j:outputPanel> 
        <a4j:outputPanel id="messageRFC"> 
         <rich:message id="rfcMessage" ajaxRendered="true" 
             for="RFC" 
             rendered="#{not userInformationController.curpRendered}"/> 
        </a4j:outputPanel> 
       </h:panelGrid> 

UPDATE

我已經在UI上的一些變化,現在我有這個

  <fieldset> 
       <legend>Datos SURI</legend> 
       <h:panelGrid columns="3"> 
        <h:outputLabel for="tipoPersona">Tipo de persona</h:outputLabel> 
        <rich:select id="tipoPersona" 
           required="true" value="#{userInformationController.tipoPersona}" 
           requiredMessage="Seleccione el tipo de persona" 
           defaultLabel="Seleccione una opción"> 
         <f:selectItems value="#{userInformationController.tipoPersonaOpciones}"/> 
         <f:ajax event="blur" render="tipoPersonaMessage"/> 
         <f:ajax event="selectitem" 
           render="outputLabelCURP inputTextCURP messageCURP 
             outputLabelRFC inputTextRFC messageRFC 
             datosPersonaFisica datosPersonaMoral" 
           listener="#{userInformationController.doRenderTipoPersona}"/> 
        </rich:select> 
        <rich:message id="tipoPersonaMessage" ajaxRendered="true" 
            for="tipoPersona"/> 


        <a4j:outputPanel id="outputLabelCURP"> 
         <h:outputLabel for="CURP" value="CURP" 
             rendered="#{userInformationController.personaFisicaRendered}"/> 
        </a4j:outputPanel>       
        <a4j:outputPanel id="inputTextCURP"> 
         <h:inputText id="CURP" required="true" 
            requiredMessage="Introduzca el CURP" 
            value="#{userInformationController.curp}" 
            rendered="#{userInformationController.personaFisicaRendered}"> 
          <f:ajax event="blur" render="curpMessage identificadorSURI" 
            listener="#{userInformationController.doSearchIdSURI}"/> 
         </h:inputText> 
        </a4j:outputPanel> 
        <a4j:outputPanel id="messageCURP"> 
         <rich:message id="curpMessage" ajaxRendered="true" for="CURP" 
             rendered="#{userInformationController.personaFisicaRendered}"/> 
        </a4j:outputPanel> 


        <a4j:outputPanel id="outputLabelRFC"> 
         <h:outputLabel for="RFC" value="RFC" 
             rendered="#{userInformationController.personaMoralRendered}"/> 
        </a4j:outputPanel> 
        <a4j:outputPanel id="inputTextRFC"> 
         <h:inputText id="RFC" required="true" 
            requiredMessage="Introduzca el RFC" 
            value="#{userInformationController.rfc}" 
            rendered="#{userInformationController.personaMoralRendered}"> 
          <f:ajax event="blur" render="rfcMessage identificadorSURI" 
            listener="#{userInformationController.doSearchIdSURI}"/> 
         </h:inputText> 
        </a4j:outputPanel> 
        <a4j:outputPanel id="messageRFC"> 
         <rich:message id="rfcMessage" ajaxRendered="true" for="RFC" 
             rendered="#{userInformationController.personaMoralRendered}"/> 
        </a4j:outputPanel> 

        <h:outputLabel for="identificadorSURI">Identificador SURI</h:outputLabel> 
        <h:inputText id="identificadorSURI" disabled="true" 
           value="#{userInformationController.identificadorSuri}"/> 

       </h:panelGrid> 
      </fieldset> 

然而,我發現,把我的Bean放在RequestScope沒有觸發h:inputText(id ='CURP'和id ='RFC'),而如果我把這個bean放入ViewScope,監聽器就會被觸發並正確處理。

我需要的是在RequestScope中實際擁有Bean,因爲我需要創建一個全新的表單,無論用戶是否更改rich:select的值。如果我將範圍保留在ViewScope中,所有數據都保存在Bean中,那麼當用戶更改選擇的值時,我將不得不刪除這些字段。

有人可以向我解釋爲什麼會發生這種情況?我無法弄清楚有什麼訣竅!

乾杯

回答

1

的請求範圍的bean被重建的每一個要求,還對每一個Ajax請求。因此,當您通過ajax將屬性更改爲非默認值(例如負責rendered屬性的屬性)時,則此更改將不會保留在後續請求中。因此,rendered屬性有效地評估爲false,並且在提交期間組件及其所有子項不再進行處理。

視圖範圍旨在解決這個問題。只要您通過ajax請求與相同的視圖進行交互,bean就會生活,直到您轉到其他視圖或重新創建視圖。至於<rich:select>更改表單的功能要求,只需在doRenderTipoPersona()方法內執行即可。

如果您確實需要堅持請求範圍,那麼您需要根據(post)構造函數中的請求參數映射手動保留bean的屬性。

+0

謝謝。這就是我想象的(ajax創建新的請求)。我所做的是將我的bean保存在View Scope中,並清除''監聽器上的所有bean的字段。 – BRabbit27 2012-04-10 15:37:25

0

而不是使用到事件功能,您可以使用單一的AJAX功能

<rich:select id="tipoPersona" 
           required="true" value="#{userInformationController.tipoPersona}" 
           requiredMessage="Seleccione el tipo de persona" 
           defaultLabel="Seleccione una opción"> 
         <f:selectItems value="#{userInformationController.tipoPersonaOpciones}"/> 
         <f:ajax event="valueChange" 
           render="tipoPersonaMessage outputLabelCURP inputTextCURP messageCURP outputLabelRFC inputTextRFC messageRFC" 
           listener="#{userInformationController.doRenderCURP}"/> 
        </rich:select> 
+0

感謝您的回答。也許我解釋自己是錯的,但問題不在** rich:select **問題出現在** h:inputText **(id ='CURP'和id ='RFC')。當'blur'事件被觸發時,我希望偵聽器能夠被調用,但事實並非如此。 – BRabbit27 2012-04-09 22:23:57

+0

ahan ..我有另一個解決方案,你可以在Inputtext上調用一個onBlur函數,你可以在那裏調用你的監聽器函數的a4j:jsFunction調用。 – 2012-04-12 10:52:21

0

那麼這是一個解決方案。

<a4j:jsFunction name="onBlurRFC" execute="RFC" render = "rfcMessage identificadorSURI" 
    action="#{userInformationController.doSearchIdSURI}" />  

    <h:inputText id="RFC" required="true" 
              requiredMessage="Introduzca el RFC" 
              value="#{userInformationController.rfc}" 
              rendered="#{userInformationController.personaMoralRendered}" onblur="onBlurRFC()"> 
           </h:inputText> 
+0

爲什麼這是一個解決方案? – 2012-04-12 11:00:37

+0

對不起,a4j:jsFunction缺失。 a4j:jsFunction將觸發一個bean上的偵聽器並呈現面板。 CURP輸入文本也一樣。 – 2012-04-13 10:52:22