[hibernate-users] GlassFish, JSR-303, Custom Annotation does not fire

Roger Varley roger.varley at googlemail.com
Sun Nov 14 15:12:48 EST 2010


Hi

I'm just starting JEE6 with Glassfish and JSF, and I'm trying to create
my first JSR-303 validation. The problem I've got is that despite the
fact that everything compiles, the validator class is never called. I've
listed the code below. (I know that the validator doesn't make logical
sense at the moment, there's more to go in it, I'm just trying to get it
to work at the moment.) Can anyone see what I'm doing wrong.

Regards

The annotation;

 package com.vasilikon.pembridge.validators;  
  
 import com.vasilikon.pembridge.entities.TelephoneType;  
 import static java.lang.annotation.ElementType.*;  
 import static java.lang.annotation.RetentionPolicy.*;  
  
 import java.lang.annotation.Documented;  
 import java.lang.annotation.Retention;  
 import java.lang.annotation.Target;  
 import javax.validation.Constraint;  
 import javax.validation.Payload;  
 
@Target({FIELD})  
@Retention(RUNTIME)  
@Constraint(validatedBy = PhoneNumberValidator.class)  
@Documented  
public @interface PhoneNumberCheck  
 {  
     String message() default "Invalid Telephone number.";  
   
     Class<?>[] groups() default {};  
     Class<? extends Payload>[] payload() default {};  
     TelephoneType value();  
 }      

The validator;

  package com.vasilikon.pembridge.validators;  
   
 import com.vasilikon.pembridge.entities.TelephoneType;  
 import javax.validation.ConstraintValidator;  
 import javax.validation.ConstraintValidatorContext;  

 public class PhoneNumberValidator implements
ConstraintValidator<PhoneNumberCheck, Object>  
  {  
      private TelephoneType type;  
    
      @Override  
      public void initialize(PhoneNumberCheck constraintAnnotation) {  
        this.type = constraintAnnotation.value();  
      }  
    
      @Override  
      public boolean isValid(Object value, ConstraintValidatorContext
context) {  
           try {  
               System.out.println("isValid called");  // Never gets
here  
               long telephoneNumber = ((Long) value).longValue();  
               if (type == TelephoneType.MOBILE) {  
                   return
isValidMobileNumber(context,telephoneNumber,type);  
               } else {  
                   return
isValidLandLineNumber(context,telephoneNumber,type);  
               }  
     
           } catch (Exception e) {  
           /* Do Nothing */  
           }  
           return true;  
       }  
     
      private boolean isValidMobileNumber(ConstraintValidatorContext
context, long telephoneNumber, TelephoneType type) {  
           /* 
            * Mobile number is exactly 8 digits long 
            * and starts 99nnnnnn 
            */  
           boolean valid =  ((telephoneNumber > 99000000)  
                               && (telephoneNumber < 999999999));  
           if (!valid) {  
               context.disableDefaultConstraintViolation();  
               context.buildConstraintViolationWithTemplate("A mobile
number must be 8 digits starting with 99")  
                   .addNode(String.valueOf(telephoneNumber))  
                   .addConstraintViolation();  
     
           }  
          return valid;  
     
       }  
     
       private boolean isValidLandLineNumber(ConstraintValidatorContext
context, long telephoneNumber, TelephoneType type) {  
          throw new UnsupportedOperationException("Not yet
implemented");  
      }  
  }     


and my backing bean (trimmed);

 @Named  
 @RequestScoped  
   
public class PhoneNumberPromptBean {  
   
     @Inject  
     PembridgeServices services;  
   
     @NotNull(message="You must select a number type")  
     private TelephoneType type;  

     @PhoneNumberCheck(TelephoneType.MOBILE)  
     private long telephoneNumber;  
   
  <Getters and Setters>  
   
      



More information about the hibernate-users mailing list