-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Need help on Hibernate Validator
PostPosted: Tue May 27, 2008 4:00 pm 
Beginner
Beginner

Joined: Tue Dec 27, 2005 1:13 pm
Posts: 25
Location: Kingston, ON, Canada
Hi, I created a validator like below. It seemed that it did not work, and there was not error message.

I use JBoss Seam 2.0.1


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



Code:
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();
   }
}


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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.