0

In a Spring MVC application, the form data validation is not happening correctly and org.springframework.validation.BeanPropertyBindingResult is not reporting any error.

@RequestMapping(value="downloadfile", method=RequestMethod.POST)
    public String submitForm(@ModelAttribute("downloadMVbinder") 
                             @Valid DownloadFileVO downloadFileVO,
                             BindingResult bindingResult,
                             Model model) {
        System.out.println("Binding result object value ==> " + bindingResult.toString());

        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<DownloadFileVO>> constraintViolations = validator.validate(downloadFileVO);
        System.out.println("Constrain Violation object value ==>" + constraintViolations.toString());

        System.out.println("Model object value ==> " + model.toString());
        System.out.println("Model object value ==> " + downloadFileVO.toString());

        if (bindingResult.hasErrors()){
            return "DownloadfilePage";
        }

The debug output is

Binding result object value ==> org.springframework.validation.BeanPropertyBindingResult: 0 errors
Constrain Violation object value ==>[ConstraintViolationImpl{interpolatedMessage='length should be between 5 and 8 characters', propertyPath=password, rootBeanClass=class com.user1.test.model.DownloadFileVO, messageTemplate='length should be between 5 and 8 characters'}, ConstraintViolationImpl{interpolatedMessage='file length cannot be greater than 44 bytes length', propertyPath=fileName, rootBeanClass=class com.user1.test.model.DownloadFileVO, messageTemplate='file length cannot be greater than 44 bytes length'}, ConstraintViolationImpl{interpolatedMessage='length should be between 5 and 8 characters', propertyPath=userName, rootBeanClass=class com.user1.test.model.DownloadFileVO, messageTemplate='length should be between 5 and 8 characters'}]
Model object value ==> {downloadMVbinder=DownloadFormVO [ipAddres=NONEuserName=password=fileName=windowsPath=], org.springframework.validation.BindingResult.downloadMVbinder=org.springframework.validation.BeanPropertyBindingResult: 0 errors}
Model object value ==> DownloadFormVO [ipAddres=NONEuserName=password=fileName=windowsPath=]

I have updated the method parameters after checking the post 0 errors. Still not getting errors in bindingResult. I have also validated the model object with javax.validation.ConstraintViolation which captures the contraint violation.

Can you kindly help in resolving why errors are not showing in bindingResult object.

Thank you!

1 Answer 1

0

I have used SpringValidatorAdapter to expose the JSR 303 Validator. The answer is based on the posts use of bindingresult and convert JSR-303 validation errors to bindingresult

@RequestMapping(value="downloadfile", method=RequestMethod.POST)

        public String submitForm(@ModelAttribute("downloadMVbinder") 
                             @Valid DownloadFileVO downloadFileVO,
                             BindingResult bindingResult,
                             Model model) {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<DownloadFileVO>> constraintViolations = validator.validate(downloadFileVO);
        SpringValidatorAdapter springValidator = new SpringValidatorAdapter(validator);
        springValidator.validate(downloadFileVO, bindingResult);

        if (bindingResult.hasErrors()){
            return "DownloadfilePage"; 
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.