-->
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.  [ 2 posts ] 
Author Message
 Post subject: Get variable data in Hbrn Validator to show in error msg
PostPosted: Wed Aug 14, 2013 3:28 am 
Newbie

Joined: Thu Jan 15, 2009 6:41 am
Posts: 9
I am development one Hibernate constraint to validate extension file. The question is I could want show in message error the bad upload file extension.

Now i have constraint look like this.

Enum to valid data.

Code:
    public enum FileType {
        JPG, PNG, GIF, BMP;
    }


Contraint.

Code:
    @Target({METHOD, FIELD, ANNOTATION_TYPE})
    @Retention(RUNTIME)
    @Constraint(validatedBy = CheckFileTypeValidator.class)
    @Documented
    public @interface CheckFileType {
   
        String message() default "{CheckFileType}";
   
        Class<?>[] groups() default {};
   
        Class<? extends Payload>[] payload() default {};
       
        FileType[] types();
       
    }


Validator.

Code:
    public class CheckFileTypeValidator implements ConstraintValidator<CheckFileType, MultipartFile> {
   
        private FileType[] types;
   
        @Override
        public void initialize(CheckFileType checkFileType) {
   
            this.types = checkFileType.types();
        }
   
            @Override
            public boolean isValid(MultipartFile multipartFile, ConstraintValidatorContext constraintValidatorContext) {
       
                for (int x = 0; x < this.types.length; x++) {
       
                    if (multipartFile.getContentType().toLowerCase().contains(this.types[x].toString().toLowerCase())) {
   
                        return true;
                    }
       
                }
       
                return false;
            }
           
        }


To use it.

Code:
    @CheckFileType(types = {FileType.JPG, FileType.PNG})
    private MultipartFile credential;


Now, What do I need to do to implement this feature?


Top
 Profile  
 
 Post subject: Re: Get variable data in Hbrn Validator to show in error msg
PostPosted: Thu Aug 15, 2013 3:53 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
If you are using Bean Validation 1.1 / Hibernate Validator 5.x, you can refer to the validated value within a Unified EL script expression:

Code:
@CheckFileType(
    types = {FileType.JPG, FileType.PNG},
    message = "Expected type to be one of {types} but was ${validatedValue.getContentType().toLowerCase()}."
)
private MultipartFile credential;


Here, "validatedValue" is the reserved name under which the validated object is made available to EL interpolation (the reference guide has some more details:
http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-interpolation-with-message-expressions).

If you're still on Bean Validation 1.0, you could add a custom violation via the ConstraintValidatorContext like this:

Code:
...
@Override
public boolean isValid(MultipartFile multipartFile, ConstraintValidatorContext constraintValidatorContext) {
    boolean isValid = ...;

    if ( !isValid ) {
        constraintContext.disableDefaultConstraintViolation();
        constraintContext.buildConstraintViolationWithTemplate(
            "Expected type to be one of " + Arrays.toString( types ) +
            " but was " + multipartFile.getContentType().toLowerCase() + "."
        )
        .addConstraintViolation();
    }

    return isValid;
}
...


You can find more details on using ConstraintValidatorContext at http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-customconstraints-validator.

Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


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

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.