5 Replies Latest reply on Nov 20, 2008 4:28 PM by francof

    force a4j:support onchange for field value from browser hist

      Seam 2.0.2 and Richfaces 3.2.1

      I don't understand how to correctly trap this validation.

      I have this field

      <s:decorate id="taxCodeDecoration">
       <h:inputText id="taxCode"
       required="true"
       label="Tax code"
       size="10"
       disabled="#{taxHome.managed}"
       maxlength="10"
       styleClass="#{invalid?'errors':''}"
       value="#{taxHome.instance.taxCode}">
       <a4j:support event="onblur" reRender="taxCodeDecoration" ajaxSingle="true" bypassUpdates="true" />
       <a4j:support event="onchange" reRender="taxCodeDecoration" ajaxSingle="true"
       action="#{taxHome.validateTaxCode}"/>
      
       </h:inputText>
      </s:decorate>


      and this action method

      public void validateTaxCode() {
      
       isUniqueSimpleKey(
       getInstance().getClass().getName(),
       "taxCode",
       getInstance().getTaxCode(),
       "This tax code already exists" ) ;
      
       }



      This is the key field for my entity and my validation is checking if the field value is unique.

      If I pick a value from the drop down list of previously entered values (from the browser's autocomplete history), the validation fires the first time and I get the error message, but then thereafter if I type the same value, onchange is not fired. I understand that. What JS event should I use to trap this validation?

      Thanks
      Franco


      I now to try to recreate the same

        • 1. Re: force a4j:support onchange for field value from browser
          nbelaevski

          Franco,

          Try onkeyup. Use eventsQueue/requestDelay attributes to protect server from flooding with requests: http://www.jboss.org/file-access/default/members/jbossrichfaces/freezone/docs/devguide/en/html/ArchitectureOverview.html#QueueandTrafficFloodProtection

          • 2. Re: force a4j:support onchange for field value from browser

            Thank you, I appreciate your guidance.

            I added autocomplete="off" to my input text to prevent use of history.

            I then tried your suggestion and wrote

            <a4j:support event="onkeyup" reRender="taxCodeDecoration" ajaxSingle="true" eventsQueue="taxEvents" requestDelay="5000" action="#{taxHome.validateTaxCode}"/>
            


            It worked like a charm!

            My only issue is this - since I have set a 5 second requestDelay,

            what if the user starts typing in 1 character and then say starts talking to a colleague, after 5 secs, the query will fire automatically even b4 an actual onchange (user tabbed out) ? - of course I can increase the requestDelay. I guess my concern is will I pass UAT?

            Thanks again, I can go to bed now :)




            • 3. Re: force a4j:support onchange for field value from browser
              ilya_shaikovsky

              using onkeyup - the request will be fired after request delay even if the filed was leaved.

              using onblur instead of onkeyup you could fire the request only after the user stoped typing and leaved input.

              • 4. Re: force a4j:support onchange for field value from browser

                Thanks again.

                I do have an onblur event that checks all the other validations (Hibernate validators) on my field in the model. I only have a custom @Required right now for this field.

                Do you advise that I put my action method taxHome.validateTaxCode on the onblur event and let the method check for required value first ?
                What if the field has other validations I want to check for like say @Length , if I put all these in my action method, guess I am breaking DRY principle, right?

                Basically, I am trying to achieve this -
                - trap all Hibernate validators on model then
                - trap duplicate value entered.

                Using onblur and then onkeyup events with the help from nbelaevski
                helped, but is that the standard way for key field validations using ajax4jsf ?


                Thanks and regards
                Franco

                • 5. Re: force a4j:support onchange for field value from browser

                  I think I can live with onkeyup.

                  Using onchange is so inconsistent in Firefox and IE -
                  In Firefox, the JSF error message from my action method would not appear always if the value was duplicate.

                  I wrote


                  <h:inputText id="taxCode" required="true"
                   label="Tax code"
                   size="10"
                   disabled="#{taxHome.managed}"
                   autocomplete="off"
                   maxlength="10"
                   styleClass="#{invalid?'errors':''}"
                   valueChangeListener="#{taxHome.recordTaxCodeChange}"
                   value="#{taxHome.instance.taxCode}" >
                  
                  <a4j:support event="onblur" reRender="taxCodeDecoration"
                   ajaxSingle="true" bypassUpdates="true" />
                  
                  <a4j:support event="onkeyup" reRender="taxCodeDecoration" ajaxSingle="true" eventsQueue="taxEvents" requestDelay="4000" ignoreDupResponces="true" action="#{taxHome.validateTaxCode}"/>
                  </h:inputText>
                  


                  I added a valuechangedListener and save the current value into a variable on action class and use that to check for uniqueness.

                  This works consistently in both browsers now.

                  Thanks for your help guys.


                  If there is anyone else who has a different solution, I would love to hear.

                  Regards
                  Franco