简体   繁体   中英

How to execute code before @InitBinder validation

I have a program that has a functionality to upload a file and then validate if it's name format is correct and save it to db.

In my main controller I am using @InitBinder for validation.

@InitBinder("uploadFileForm")
    protected void initBinderUploadForm(WebDataBinder binder) {
        binder.setValidator(fileNameFormatValidator);
    }

In my validator method I am using this code snippet:

static void validateFileName(String fileUploadKey, MultipartFile file, Errors errors) {
        Matcher validFilenameMatcher = VALID_FILE_NAME.matcher(file.getOriginalFilename());
        if (!validFilenameMatcher.matches()) {
            errors.rejectValue(fileUploadKey, null, null, SOME_REGEX);
        }
    }

What I want to do is, I want to format file name (like replacing some characters in file name) and then use validator class. Thus I need to change file name before the validation.

How can I achieve editing file name before validating the format with @InitBinder ?

Edit: Noone answers? Or the question isn't clear ?

Why not use WebDataBinder 's addCustomFormatter like you did for adding a validator ?

@InitBinder("uploadFileForm")
protected void initBinderUploadForm(WebDataBinder binder) {
    binder.addCustomFormatter(fileNameFormatter); 
    binder.setValidator(fileNameFormatValidator);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM