6 Replies Latest reply on Oct 21, 2012 11:51 PM by Jose Alvarez de Lara

    h:selectOneMenu valueChangeListener does not work

    Jose Alvarez de Lara Novice

      I have some h:selectOneMenu in my xhtml file which I have to validate.

       

      Here is an example of my code,

      <h:panelGroup id="inCCTypePanel">
          <h:form id="inCCTypeForm">
              <h:panelGrid  rendered="#{carritoBean.carritoSelected and !(carritoBean.pedidoAbonado or carritoBean.pedidoCancelado)}">
                  <rich:panel style="width:150px;">
                      <f:facet name="header">Tipo de tarjeta</f:facet>
                      <h:selectOneMenu id="cctId" value="#{carritoBean.cCard}" valueChangeListener="#{carritoBean.changeCCType}"
                          required="true" requiredMessage="Selected credit card type is required.">
                          <f:selectItem itemValue="" itemLabel="Default" />
                          <f:selectItem itemValue="american" itemLabel="American Express" />
                          <f:selectItem itemValue="dinner" itemLabel="Dinner's Club" />
                          <f:selectItem itemValue="discover" itemLabel="Discover" />
                          <f:selectItem itemValue="master" itemLabel="Master Card" />
                          <f:selectItem itemValue="visa" itemLabel="Visa" />
                          <a4j:ajax event="valueChange" render="validateCCTypePanel" execute="@this"/>
                      </h:selectOneMenu>
                  </rich:panel>
                  <a4j:outputPanel id="validateCCTypePanel">
                      <h:panelGrid rendered="#{carritoBean.cCardSelected and !(carritoBean.pedidoAbonado or carritoBean.pedidoCancelado)}">
                          <h:inputHidden id="idCCType" value="#{carritoBean.cCard}" >
                              <f:validator validatorId="creditCardTypeValidator" />
                          </h:inputHidden>
                          
                          <a4j:commandButton value="Credit Card Type Validate" action="#{carritoBean.validateCCType}" >
                              <f:ajax execute="@form" render="@form :outCCDataPanel :outCCDataForm" />
                          </a4j:commandButton>
                      </h:panelGrid>
                  </a4j:outputPanel>
              </h:panelGrid>
          </h:form>
      </h:panelGroup>
      

       

      and here is the bean code,

      public void changeCCType(ValueChangeEvent event) {
          cCard = null != event.getNewValue() ? (String)event.getNewValue() : null;
          System.out.println("Selected Credit Card: " + cCard);
          cCardSelected = (cCard != null);
      }
          
      public void validateCCType() {
          showCCType = cCardSelected;
      }
      

       

      What I pretend is to catch cCard in the valueChangeListener event

      and once it is set validate.

       

      But I do not why this does not work.

       

      Any suggestion will be appreciated.

       

      Thanking in advance,

      Jose

        • 1. Re: h:selectOneMenu valueChangeListener does not work
          iabughosh Master

          try event "change" instead of "valueChange" at a4j:ajax tag.

           

          regards.

          • 2. Re: h:selectOneMenu valueChangeListener does not work
            Jose Alvarez de Lara Novice

            Hi Ibrahim,

             

            Thank you for your quick reply.

             

            I did what you say but still does not work.

             

            I am guessing when I select an option which must be different

            than Default, valueChangeListener="#{carritoBean.changeCCType}"

            must set cCard to that option and cCardSelected is set to true.

             

            On the other hand,

            <a4j:ajax event="change" render="validateCCTypePanel" execute="@this" />

            let me see the commandButton due all the boolean vars are set to true.

             

            This is what I hope but it does not work.

             

            Regards,

            Jose

            • 3. Re: h:selectOneMenu valueChangeListener does not work
              Siva P Master

              Hello Jose Alvarez de Lara,

               

              Try like this,

              Suppose if you are not able to use Value change listener ,try with ajax support tag and ActionLiener.You set some values in backend bean and render it to jsp page.it will work.

               

              Please see the below example for your reference,Its calling backend and displaying SOP in console....

               

              <h:selectOneMenu id="cctId" value="#{docLibraryHome.countryText}"

                                  required="true" requiredMessage="Selected credit card type is required.">

                                  <f:selectItem itemValue="" itemLabel="Default" />

                                  <f:selectItem itemValue="american" itemLabel="American Express" />

                                  <f:selectItem itemValue="dinner" itemLabel="Dinner's Club" />

                                  <f:selectItem itemValue="discover" itemLabel="Discover" />

                                  <f:selectItem itemValue="master" itemLabel="Master Card" />

                                  <f:selectItem itemValue="visa" itemLabel="Visa" />

                                  <a:support event="onchange"  limitToList="true" render="validateCCTypePanel" execute="@this"

                                      actionListener="#{docLibraryHome.changeValuesForCountryActionListener}"/>

                              </h:selectOneMenu>

              _________________

               

              public void changeValuesForCountryActionListener(ActionEvent event)

                  {

                      System.out.println("Action Listener Called..........:"+event.getSource().toString());

                     

                  }

               

              ____________________________

               

              123.bmp

               

              1234.bmp

               

              Thanks,

              Siva

              • 4. Re: h:selectOneMenu valueChangeListener does not work
                Jose Alvarez de Lara Novice

                Hi Silva,

                 

                Thanks for your interesting and reply.

                 

                When you say ajax support tag, are you meaning <a4j:support>?

                I can't understand because as you know RF 4.x has not that tag,

                 

                "The a4j:support has been replaced by a4j:ajax since RichFaces 4.x.

                You seem to be reading RichFaces 3.x documentation or just have upgraded RichFaces 3.x to 4.x.

                This is a pretty major step where a lot of things are been changed.

                Read the migration guide (in this particular case, it's described in

                a4j components) also read the new RF4 component reference."

                 

                I have gotten this text from here,

                http://stackoverflow.com/questions/5402697/a4jsupport-tag-not-found-using-jsf-2

                so I am afraid we will have to find another solution to my problem.

                 

                Anyway thanks,

                Jose

                 

                El mensaje fue editado por: Jose Alvarez de Lara

                • 5. Re: h:selectOneMenu valueChangeListener does not work
                  Siva P Master

                  Hi  Jose,

                   

                  I think  below link helps you,

                   

                  http://www.developer.am/richfaces/?page=a4j-ajax

                   

                  http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=actionListener

                  if ou are not able to resolve it send an email to richfaces team...

                   

                  Thanks,

                  Siva

                  • 6. Re: h:selectOneMenu valueChangeListener does not work
                    Jose Alvarez de Lara Novice

                    Hi Siva,

                     

                    Thanks for the information.

                     

                    Here is the way I have used to resolve the issue,

                     

                    the jsf view,

                     

                    <h:panelGroup id="inCCTypePanel">

                       <h:form id="inCCTypeForm">

                          <h:panelGrid  rendered="#{carritoBean.carritoSelected and !(carritoBean.pedidoAbonado or carritoBean.pedidoCancelado)}">

                             <rich:panel style="width:150px;">

                                <f:facet name="header">Tipo de tarjeta</f:facet>

                                <h:selectOneMenu id="cctId" value="#{carritoBean.cCard}">

                                      <f:selectItem itemValue="" itemLabel="Select a C. C. Type" />

                                      <f:selectItem itemValue="american" itemLabel="American Express" />

                                      <f:selectItem itemValue="dinner" itemLabel="Dinner's Club" />

                                      <f:selectItem itemValue="discover" itemLabel="Discover" />

                                      <f:selectItem itemValue="master" itemLabel="Master Card" />

                                      <f:selectItem itemValue="visa" itemLabel="Visa" />

                                      <a4j:ajax listener="#{carritoBean.changeCCType}" render="validateCCTypePanel"/>

                                </h:selectOneMenu>

                             </rich:panel>

                             <a4j:outputPanel id="validateCCTypePanel">

                                <h:panelGrid rendered="#{carritoBean.cCardSelected and !(carritoBean.pedidoAbonado or carritoBean.pedidoCancelado)}">

                                   <h:inputHidden id="idCCType" value="#{carritoBean.cCard}" >

                                      <f:validator validatorId="creditCardTypeValidator" />

                                   </h:inputHidden>

                                                                           

                                   <a4j:commandButton value="Credit Card Type Validate" action="#{carritoBean.validateCCType}" >

                                      <a4j:ajax execute="@this" render="@form :outCCDataPanel :outCCDataForm" />

                                   </a4j:commandButton>

                                </h:panelGrid>

                             </a4j:outputPanel>

                          </h:panelGrid>

                       </h:form>

                    </h:panelGroup>

                     

                    and the back end bean is as follows,

                     

                    @ViewScoped

                    @Named("carritoBean")

                    public class CarritoBean implements Serializable {

                     

                    --------------------------------------

                    --------------------------------------

                     

                    public void changeCCType() {

                            System.out.println("changeCCType. Selected cCard.......: " + cCard);

                            cCardSelected = (cCard != null);

                    }

                     

                    public void validate CCType() {

                            showCCType = cCardSelected;

                    }

                     

                    being cCardSelected and showCCType boolean properties

                     

                    Everything is ok but I am having troubles with the beans scoped.

                     

                    At the first time results logic to think I need to use ViewScoped

                    because the data is deployed in the jsf view dynamically from top to

                    down and I need to hold the information requested in every step of

                    the deployment.

                     

                    But I use communication between beans passing a HashMap. The

                    balusC FacesUtil class resolve this question but I do not know why

                    I can not remove the session value once the HashMap is in the

                    consumer bean.

                     

                    And this is the point I am working on.

                    Any suggestion will be appreciated.

                     

                    Regards,

                    Jose