Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: ReUse a EmailValidator
PostPosted: Thu Apr 05, 2012 1:46 pm 
Newbie

Joined: Thu Apr 05, 2012 1:40 pm
Posts: 2
Hi,

I want to create my own Annatotation Constraint, because I hava a String which contains a list of emails seperated by a comma.

I follow the doc from here : http://docs.jboss.org/hibernate/validat ... aints.html

In my project I use Hibernate Validator as Provider for Bean validation. And I notice that Hibernate provide an @Email constraint.

Is it possible to call the org.hibernate.validator.constraints.impl.EmailValidator::isValid from my own Validator class ?

I made the following and its work:

Code:
public class EmailsValidator implements ConstraintValidator<Emails, String> {

private EmailValidator emailValidator = new EmailValidator();

@Override
public void initialize(Emails constraintAnnotation) {
}

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
    System.out.println("EmailsValidator::isValid");
    if (value == null || value.isEmpty()) {
        return true;
    }
    final String[] emails = value.split(",\\s*");
    for (String anEmail : emails) {
        System.out.println("anEmail = " + anEmail);
        if (!emailValidator.isValid(anEmail, context)) {
            return false;
        }
    }
    return true;
}
}


Is it the most efficient way to make it ?

Bye.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.