6 Replies Latest reply on Oct 17, 2011 8:54 AM by pushkaraj20885

    a4j support and immediate

    trajber

      Hi all,

      I was trying to make a text field with a4j:support an immediate component. But when I set it with immediate="true" the managed bean does not receive the value sent via ajax, it is null. Why ?

      test.jsf

      <h:inputText value="#{test.name}">
       <f:validateLength minimum="2" />
       <a4j:support event="onblur" actionListener="#{test.doSomething}"
       reRender="parent"
       immediate="true" />
      </h:inputText>
      


      Test.java
      public class Test {
       private String name;
      
       public String getName() {
       return name;
       }
      
       public void setName(String name) {
       this.name = name;
       }
      
       public void doSomething(ActionEvent actionEvent) {
       System.out.println("OOPS:"+name);
       }
      }
      


      output...
      OOPS:null
      


      As you can see, the method is invoked properly but the value is not sent!!!
      Removing the immediate attribute everything goes normal.


        • 1. Re: a4j support and immediate
          holstead

          immediate="true" means it is going to ignore validation.

          From the documentation: "True means, that the default ActionListener should be executed immediately (i.e. during Apply Request Values phase of the request processing lifecycle), rather than waiting until the Invoke Application phase"

          I usually only use immediate for cancel/reset buttons.

          • 2. Re: a4j support and immediate

             

            "holstead" wrote:
            immediate="true" means it is going to ignore validation.

            From the documentation: "True means, that the default ActionListener should be executed immediately (i.e. during Apply Request Values phase of the request processing lifecycle), rather than waiting until the Invoke Application phase"

            I usually only use immediate for cancel/reset buttons.


            What you say is not a primary behaviour, but just a useful side effect. immediate="true" means that all activities attached to this particular component DO happen on the second JSF lifecycle phase. (ie. validation, update model and application invocation are not bypassed, not just happen earlier)

            You can apply immediate="true" not only to the command button, but to any form components as well.
            The way how JSF walks through the lifecycle depends of do you have immediate="true" on the command component (button or link) or not.

            1) If command component HAS NO immediate="true":

            * on the 2nd phase of the lifecycle, the validation, update model happens on the form elements that have immediate="true"
            ** if validation is OK the model for those components is updated and JSF goes to the rest phases for other form fields.
            ** otherwise, processing of other form elements and actions are skipped and JSF goes to the 6th (render response) phase

            2) If command component HAS immediate="true":

            * on the 2nd phase of the lifecycle, the validation, update model happens for the form elements that have immediate="true"
            ** if validation is OK the action and action listener for the command component are performed on the second phase also. Then JSF goes to the 6th phase
            ** otherwise (if validation is failed), the action and action listener are not performed and JSF goes to the 6th phase


            Your case - using immediate="true" for cancel button is a special case for #2 when you have immediate="true" only on this button




            • 3. Re: a4j support and immediate
              trajber

              OK, I undestand; When immediate=true the validation occours in another step in JSF's life cycle. But how can I bypass the validation step using ajax ?

              Example: I need to execute a method (using ajax) that updates an field in my form , but if one or more fields in the form has an validation rule the ajax request is not made.

              test.jsf

              
              <h:form id="parent">
               <h:messages/>
               <h:inputText value="#{test.name}">
               <a4j:support event="onblur"
               reRender="repeater"
               actionListener="#{test.action}"
               />
               </h:inputText>
               <h:inputText value="#{test.sex}" id="sex" validatorMessage="error" required="true">
               <f:validateLength minimum="4" />
               </h:inputText>
               <h:outputText value="#{test.name}" id="repeater"/>
               <h:commandButton action="#{test.doSomething}" />
               </h:form>
              
              


              As you can see, the field sex has a validation (minimum 4 digits).

              And now, the managed bean.

              Test.java
              
              public class Test {
               private String name;
               private String sex;
              
              
              
               public String getSex() {
               return sex;
               }
              
               public void setSex(String sex) {
               this.sex = sex;
               }
              
               public String getName() {
               return name;
               }
              
               public void setName(String name) {
               this.name = name;
               }
              
               public void doSomething() {
               System.out.println("OOPS:"+name);
               }
              
               public void action(ActionEvent actionEvent) {
               name = "Sr."+name;
               FacesContext.getCurrentInstance().renderResponse();
               }
              }
              
              


              But the ajax request on blur event is not made unless the validation of field "sex" is satisfied (with 4 digits).

              Why ? Is there anyway to bypass the validation of other components ? How ?



              • 4. Re: a4j support and immediate

                 

                "trajber" wrote:
                OK, I undestand; When immediate=true the validation occours in another step in JSF's life cycle. But how can I bypass the validation step using ajax ?


                As soon as the h:commandButton has no immediate="true", you cannot avoid the validation on the second inputText even you set immediate="true" to the first inputText. As a result, the first inputText will be processed and the test bean will be populated with new 'name'. However, then, the actionListener will NOT be invoked because the validation is failed on the second inputText

                So, in this particular cases you have:
                1. add ajaxSingle="true" to the a4j:support
                OR
                2. wrap h:inputText (the one with a4j:support) with a4j:region

                BTW, as far as you use h:messages instead of rich:messages, you cannot see the validation error produced by the second inputText

                • 5. Re: a4j support and immediate
                  trajber

                  Thank you. It works

                  • 6. Re: a4j support and immediate
                    pushkaraj20885

                    Hi

                     

                    1. add ajaxSingle="true" to the a4j:support

                    Its work for my case as well

                     

                    Thanks

                    Sergey Smirnov