1 Reply Latest reply on Apr 23, 2009 6:14 PM by Arbi Sookazian

    Problem with Custom Validator

    gopi balagala Newbie
      Hi,


      I'm trying to implement a new validator. It checking in blank spaces and special characters except(_.) validations


      package com.manam.mobee.validation;



      import java.lang.annotation.Documented;
      import java.lang.annotation.Retention;
      import java.lang.annotation.RetentionPolicy;
      import java.lang.annotation.ElementType;
      import java.lang.annotation.Target;

      import org.hibernate.validator.ValidatorClass;



      /**
      *
      * @author Gopi
      *
      */
      @ValidatorClass(UserNameImpl.class)
      @Target({ElementType.METHOD, ElementType.FIELD})
      @Retention(RetentionPolicy.RUNTIME)
      @Documented
      public @interface UserNameTEST {
           
           
           String message() default "Invalid User Name";

      }

      -----------------

      package com.manam.mobee.validation;


      import java.io.Serializable;
      import java.util.regex.Matcher;

      import org.hibernate.validator.Validator;



      public class UserNameImpl implements Validator<UserNameTEST>, Serializable{

           /**
            *
            */
           private static final long serialVersionUID = -8872111914011575284L;
           private java.util.regex.Pattern pattern;

           public boolean isValid(Object value) {
                
                System.out.println("UUUUUUUUUUUUUUUUUUUUUUU");
                
                String s=(String) value;
                
                if ( s == null )
                     return true;
                if ( !( value instanceof String ) ){
                     System.out.println("FFFFFFFFFFFFFFFFFFFFF" +s);     
                   return false;
                }
                if(s.startsWith(".")||s.startsWith("_")||s.endsWith(".")||s.endsWith("_"))
                     {
                          System.out.println("************* " +s);
                          return true;
                     }
                     System.out.println("NNNNNNNNNNNNNNN" +s);
                     if ( s.length() == 0 ) return true;
                     Matcher m = pattern.matcher( s );
                     return m.matches();
           }
                
           public void initialize(UserNameTEST parameters) {
                // TODO Auto-generated method stub
                
                pattern = java.util.regex.Pattern.compile("[a-zA-Z0-9\\-_\\.]++(\\.[a-zA-Z]++)*",
                          java.util.regex.Pattern.CASE_INSENSITIVE);
           
                
           }

           

      }



      -----------


      The new validator is not working. The framework doesn't call "initialize" nor isValid because no System.out.println. Althought when I try to use an hibernate prebuild validator it works... that is, when I replace @UserNameTEST  annotation for example with @Email, then the value is validated and validation is working properly.