9 Replies Latest reply on Jul 9, 2012 3:40 PM by Ernesto Hernandez

    How to skip validation when clicking a selectOneMenu to update or clear a second one?

    Ernesto Hernandez Newbie

      Hi guys!

       

      Im working right now with two selectOneMenu and when I click the first one and select an item, the second one get updated showing some items. Well this is a normal behavior.

      In the first selectOneMenu I have a default item, so the user must to select an item in order to update the second selectOneMenu other than the default. Now if the user selects the default item in the first one, the second one has to be clear.

      Since Im validating these components, when I select the default item the validator fires and I cannot update the second one in order to clear the data stored in it.

       

      I have read something about the immediate = "true" but that applies only for buttons and for input components, but speaking about input components is not my case, since I don´t want to validate that component (the first selectOneMenu), well yes, but only when I submit the form but not yet.

      I put the immediate="true" inside the ajax call but it seems it doesn't work.

       

      Does anybody know how can I achieve this: select the default item in one selectOneMenu and clear the second selectOneMenu without validation stops this?

       

      Thanks in advance! = )

        • 1. Re: How to skip validation when clicking a selectOneMenu to update or clear a second one?
          Brendan Healey Master

          Some of the things you're saying aren't quite right, but you need to post the code up if you want to resolve the problem.

          The usual problem is that the first selectOneMenu doesn't render= the second.

           

          Regards,

          Brendan.

          • 2. Re: How to skip validation when clicking a selectOneMenu to update or clear a second one?
            liumin hu Master

            did you try execute=@this?i think there is a tutorial about this.

            • 3. Re: How to skip validation when clicking a selectOneMenu to update or clear a second one?
              Ernesto Hernandez Newbie

              Hi guys!

               

              This is a snapshot of the code I have:

               

                                                 <h:selectOneMenu id="productos" 
                                                      required="true" value="#{sesion.idProducto}" requiredMessage="El CAMPO PRODUCTO ES REQUERIDO."
                                                      valueChangeListener="#{catalogServices.getRequisitionTypebyProductId}">
                                                      <f:selectItem itemLabel="-- Seleccione una opcion --" />
                                                      <f:selectItems value="#{sesion.variables.products.items}"
                                                          var="prod" itemLabel="#{prod.description}" itemValue="#{prod.id}" />
                                                      <a4j:ajax event="valueChange" 
                                                          render="tiposSolicitud" execute="@this">
                                                      </a4j:ajax>     
                                                      <f:validateRequired />
                                                  </h:selectOneMenu>
                                              <rich:message for="productos" />
              
                                                 <h:selectOneMenu id="tiposSolicitud"
                                                      value="#{sesion.idTipoSolicitud}" requiredMessage="El CAMPO TIPO DE SOLICITUD ES REQUERIDO."
                                                      valueChangeListener="#{catalogServices.getInquiryByFilter}"
                                                      required="true">
                                                      <f:selectItem itemLabel="-- Seleccione una opcion --" />
                                                      <f:selectItems value="#{sesion.tiposSolicitud}" var="tipoSol"
                                                          itemLabel="#{tipoSol.description}" itemValue="#{tipoSol.id}" />
                                                      <a4j:ajax event="valueChange" render="tramites"
                                                          execute="@this" />
                                                      <f:validateRequired />
              

               

              So what I want to get working basically is, if I select the first item in "productos" which is just like a default item ('-- Seleccione una opcion --'), I want the second component "tiposSolicitud" to be clear in case if it has a selected item.

              But now that doesn't work because when I do that, the validation fires up and tells me that I need to select an item, since the first item doesn't have an itemValue and it is checked like required.

               

              I hope to be understandable.

               

              Thanks!

              • 4. Re: How to skip validation when clicking a selectOneMenu to update or clear a second one?
                Brendan Healey Master

                which validation fails, the first or second selectOneMenu? Anyway, I think you need to give

                <f:selectItem itemLabel="-- Seleccione una opcion --" /> an itemValue

                that is guaranteed to not occur in the selectItems, then it won't fail validation. Then when the

                special value is detected in the VCL #{catalogServices.getRequisitionTypebyProductId}

                you can clear #{sesion.idTipoSolicitud}. When the 2nd selectOneMenu is rerendered

                it won't have a value and the problem should be solved.

                 

                Regards,

                Brendan.

                • 7. Re: How to skip validation when clicking a selectOneMenu to update or clear a second one?
                  Ernesto Hernandez Newbie

                  Hi Brendad!

                   

                  You are right! If a give an itemValue to <f:selectItem itemLabel="-- Seleccione una opcion --" /> let's say itemValue="0", the validation won't occur and, as you said, when the special value is detected in the

                  VCL #{catalogServices.getRequisitionTypebyProductId} I can clear #{sesion.idTipoSolicitud} but, what if I do a Submit having selected the <f:selectItem itemLabel="-- Seleccione una opcion --" /> item?

                  It should tell me that "productos" is required, since I don't have a selected item, different from "-- Seleccione una opcion --". Do you know what I mean?

                   

                  Or I think I could do what you say and validate the required validation in the bean side, sending a FacesMessage if I detect the itemValue has "0" as its value.

                   

                  What do you think?

                  • 8. Re: How to skip validation when clicking a selectOneMenu to update or clear a second one?
                    Brendan Healey Master

                    Why not, instead of using required="true" use a custom validator, then you can do what you want. The way I'd do this

                    myself would be to have

                     

                    <f:selectItem itemLabel="-- Seleccione una opcion --" noSelectionOption="true"/>

                     

                    and then if a user selects something, well, so be it and the 2nd menu is populated. I can't really see a great usefulness

                    of being able to clear any selection made, but if you need to you could have a Clear button or link on the page.

                    • 9. Re: How to skip validation when clicking a selectOneMenu to update or clear a second one?
                      Ernesto Hernandez Newbie

                      I agree with you, but this is a requirement from QA, you know how the testers are.

                       

                      Using the noSelectionOption="true" what if the user selects the first item ""-- Seleccione una opcion --"" ? It won't call to the bean method, am I wrong?