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 ?