Announcement Announcement Module
Collapse
No announcement yet.
username validation in spring roo Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • username validation in spring roo

    HI............
    I am new to spring roo.Can anyone tell me how to do username validation in roo so that it will not take duplicate username and it will show error message..
    Last edited by Ktanaya; Aug 2nd, 2010, 06:17 AM.

  • #2
    That's what I did:

    Code:
    @Configurable
    @Entity
    @RooJavaBean
    @RooToString
    @RooEntity
    @UniqueEmail
    public class User {
    
    	@Email
    	@NotNull
    	private String email;
    
           ...
    }
    Code:
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    @Documented
    @Constraint(validatedBy = UniqueEmailValidator.class)
    public @interface UniqueEmail {
    
    	String message() default "Email is already registered";
    
    	Class<?>[] groups() default {};
    	
    	public abstract Class<? extends Payload>[] payload() default {};
    
    }
    Code:
    public class UniqueEmailValidator implements
    		ConstraintValidator<UniqueEmail, User> {
    
    	@Override
    	public void initialize(UniqueEmail constraintAnnotation) {
    
    	}
    
    	@Override
    	public boolean isValid(User value, ConstraintValidatorContext context) {
    		int existing = (Integer) User
    				.entityManager()
    				.createQuery(
    						"select count(u) from User u where email = :email")
    				.setParameter("email", value.getEmail().toLowerCase()).getSingleResult();
    
    		return existing==0;
    	}
    
    }

    Comment

    Working...
    X