2012-04-09 193 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