2 Replies Latest reply on Jul 23, 2009 5:55 PM by vitorsouzabr

    Seam + a4j:support re-rendering + Hibernate Validator @Pattern

    vitorsouzabr

      Hello,


      Maybe this should be sent in Hibernate's forum or in Ajax4J's forum, but since Seam is the one that puts everything together and I got the idea of using a4j:support for AJAX validation in the Reference guide for Seam, I'm posting here. Sorry if misplaced.


      I have a form with AJAX Validation like this:


      <s:decorate id="zipCodeDecoration" template="/resources/templates/#{applicationInformation.decorator}/formfield.xhtml">
           <ui:define name="label"><h:outputText value="#{messages['manageSpiritists.form.zipCode']}" /></ui:define>
           <h:inputText id="zipCode" value="#{selectedEntity.address.zipCode}" size="10" disabled="#{readOnly}">
                <a4j:support event="onblur" reRender="zipCodeDecoration" bypassUpdates="false" />
                <rich:jQuery selector="#zipCode" query="mask('99999-999')" timing="onload" />
           </h:inputText>
      </s:decorate>




      An on the zipCode property in the Address class, I had the Hibernate Validator annotation @Pattern(regex = "[0-9]{5}-[0-9]{3}"), representing the Brazilian ZIP code format: 99999-999.


      With this regular expression, the form does not accept null values. If I leave the field blank, it warns me when I leave the field (onblur) and it throws an exception if I try to submit the form.


      I tried changing the expression to () or ([0-9]{5}-[0-9]{3}) (the () in the beginning would accommodate for empty strings. This way it accepts sending the form without a value in the field (no exception thrown anymore), but it still warns me onblur.


      A note on Seam Text: in the new regular expression above, where there is or , there should be a pipe: |. I tried putting \| in the expression, but it kept giving me an error. In other words, Seam Text doesn't accept |\||. Close parenthesis...


      What's the right way to indicate that I want either a null/empty field, or a field with the specified pattern?


      Thanks,


      Vítor Souza

        • 1. Re: Seam + a4j:support re-rendering + Hibernate Validator @Pattern
          cmoerz.seam.ny-central.org

          Hi there,


          what I did to get it going: I did use @Pattern, but I extended it with a


          ||()
          



          this way, it also permits an empty string. E.g. my phone number pattern looks like this:


          @Pattern(regex="^((\\+\\d{1,3}(-| )?\\(?\\d\\)?(-| )?\\d{1,5})|(\\(?\\d{2,6}\\)?))(-| )?(\\d{3,4})(-| )?(\\d{4})(( x| ext| \\/|\\/)\\d{1,5}){0,1}$||()",
                              message="Invalid phone number")
          



          hth
          chris

          • 2. Re: Seam + a4j:support re-rendering + Hibernate Validator @Pattern
            vitorsouzabr
            Hi, thanks for the response! I didn't touch this project for quite a while, and now that I've resumed working on it, I tried your suggestion, but it still didn't work.

            I also tried the pattern

            [0-9]{5}-[0-9]{3}|^$

            but also no good. Funny thing is

            "".matches("[0-9]{5}-[0-9]{3}|^$")

            returns true. The pattern is correct. Someone else is screwing up or maybe Hibernate Validator doesn't work like String.matches()...

            Thanks anyways,

            Vítor Souza