10 Replies Latest reply on Nov 10, 2008 12:50 PM by jamesjmp

    Need Help on Hibernate Validator

    lcbdl888

      Hi, I created a custom hibernate validator like this. It seemed that the validator was not called. There was no error message at all.


      Can anybody give any idea?


      Thanks.


      Declare:

      @Documented
      @ValidatorClass(PhoneValidator.class)
      @Target({METHOD, FIELD})
      @Retention(RUNTIME)
      public @interface PhoneValid {
           String message() default "{validator.phone}";
      }


      Implement:



      public class PhoneValidator implements Validator<PhoneValid>, Serializable {
           private static final long serialVersionUID = -1049632944485567414L;
           private Pattern pattern;

           public void initialize(PhoneValid parameter) {
                System.out.println(parameter.message());
                pattern = Pattern
                          .compile("^(\\(?\\d\\d\\d\\)?)?(
      -\\.)?\\d\\d\\d( -\\.)?\\d{4,4}(( -\\.)?[ext\\.]+ ?\\d+)?$");
           }

           public boolean isValid(Object value) {
                if (value == null)
                     return true;
                if (!(value instanceof String))
                     return false;
                String string = (String) value;
                if (string.length() == 0)
                     return true;
                Matcher m = pattern.matcher(string);
                return m.matches();
           }
      }


      Annotation in the Entity class:

      @Column(name="secondary_phone", length=40)
           @PhoneValid
           private String secondaryPhone;


        • 1. Re: Need Help on Hibernate Validator

          Put backticks around your code so we can read it!


          I just got a custom validator to work not too long ago, so if you fix your post I might be able to help.

          • 2. Re: Need Help on Hibernate Validator
            @Column(name="secondary_phone", length=40) 
            @PhoneValid private 
            String secondaryPhone;
            



            @Documented 
            @ValidatorClass(PhoneValidator.class) 
            @Target({METHOD, FIELD}) 
            @Retention(RUNTIME) 
            public @interface PhoneValid { 
              String message() default "{validator.phone}"; 
            }
            
            



            public class PhoneValidator implements Validator<PhoneValid>, Serializable 
            { 
            private static final long serialVersionUID = -1049632944485567414L; private Pattern pattern; 
            public void initialize(PhoneValid parameter) 
            { 
                System.out.println(parameter.message()); 
                pattern = Pattern .compile("^(\\(?\\d\\d\\d\\)?)?( -\\.)?\\d\\d\\d( -\\.)?\\d{4,4}(( -\\.)?[ext\\.]+ ?\\d+)?$"); 
            } 
            
            public boolean isValid(Object value) { 
                if (value == null) return true; 
                if (!(value instanceof String)) return false; 
                String string = (String) value; 
                if (string.length() == 0) return true; 
                Matcher m = pattern.matcher(string); 
                return m.matches(); 
              } 
            }
            

            • 3. Re: Need Help on Hibernate Validator
              gjeudy

              You should really use a Code Block formatting option to format your code in your post.


              It looks like you wish to validate using a regular expression, then why not use the built-in

              
              org.hibernate.validator.Pattern

              annotation ?

              • 4. Re: Need Help on Hibernate Validator
                lcbdl888

                Thank you Matt for your formatting.


                To Guillaume, I just want to create a sample validator to study it.

                • 5. Re: Need Help on Hibernate Validator
                  christian.bauer

                  Please don't bump up your question again. Read our forum policy. And more importantly, ask Hibernate questions on the Hibernate forum.

                  • 6. Re: Need Help on Hibernate Validator
                    lcbdl888

                    Here is the code in the page. Is the bug here?


                    
                    <s:decorate id="primaryPhoneDecoration" template="layout/edit.xhtml">
                    
                        <ui:define name="label">Primary Phone:</ui:define>
                    
                        <h:inputText id="primaryPhone" 
                    
                                   size="40"
                    
                              maxlength="40"
                    
                                  value="#{personHome.instance.primaryPhone}">
                    
                            <a:support event="onblur" reRender="primaryPhoneDecoration" bypassUpdates="true"/>
                    
                        </h:inputText>
                    
                    </s:decorate>
                    
                    

                    • 7. Re: Need Help on Hibernate Validator
                      lcbdl888

                      and the edit.xhtml is standard generated file.


                      
                      <div class="prop">
                      
                              <s:label styleClass="name #{invalid?'errors':''}">
                      
                                  <ui:insert name="label"/>
                      
                                  <s:span styleClass="required" rendered="#{required}">*</s:span>
                      
                              </s:label>
                      
                              <span class="value #{invalid?'errors':''}">
                      
                                  <s:validateAll>
                      
                                      <ui:insert/>
                      
                                  </s:validateAll>
                      
                              </span>
                      
                              <s:message styleClass="error errors"/>        
                      
                          </div>
                      
                      

                      • 8. Re: Need Help on Hibernate Validator
                        lcbdl888

                        Christian Bauer wrote on May 28, 2008 16:09:


                        Please don't bump up your question again. Read our forum policy. And more importantly, ask Hibernate questions on the Hibernate forum.


                        This is not Hibernate problem. This is a Seam bug I think. There are 2 source folder in the workspace, one is src/action, another is src/model. If the annotation interface and validator class are in the src/action folder, it doesn't work; if they are in src/model folder, it works. Why?

                        • 9. Re: Need Help on Hibernate Validator
                          toby.tobias.hill.gmail.com

                          I had same symptoms (it appeared that validator was not called). My problem might be your problem was well.


                          I wrongly assumed that a converter was being called after a validator. This is not the case. Hence the value provided to the isValid() method was already converted ... and as a consequence value instanceof String evaluated to false (i.e. an early exit before the actual regexp was tested for a match).


                          Hope this can get you back on track (if this is still a problem for you).

                          • 10. Re: Need Help on Hibernate Validator

                            If you want to check a validator I suggest you to invoke it with one of this two ways:




                                 <h:inputText
                            ...
                            >
                                            <f:validator validatorId="yourValidator" />
                                 </h:inputText>
                            



                            or as input text has a validator attribute this way:


                            <h:inputText 
                            ...
                            validator="#{yourValidator.validate}" required="false">
                            </h:inputText>
                            


                            If you use the second way you must have a Name annotation in your validation component, to let it be invoked as a seam component.


                            Hope it helps your issue