0

I have a controller with validated form parameters:

@PostMapping("/report")
public String reportSubmit(@Valid @ModelAttribute HomeFormInput homeFormInput, Model model, BindingResult bindingResult) {
    if (bindingResult.hasErrors()) {
        return "home";
    }

The form contains an input field that has validation rule:

@Data
public class HomeFormInput {
    @NotEmpty(message = "You must provide at least one id")
    String input;
    String email;
}

When I try to test it by leaving the id field empty it throws BAD REQUEST and doesn't even enters the controler's method ( I tried to debug it). Log says the following:

2021-01-17 20:32:51.705 DEBUG 20412 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.fiserv.report.generator.controller.ReportController#homeSubmit(HomeFormInput, Model, BindingResult) 2021-01-17 20:32:51.789 DEBUG 20412 --- [nio-8080-exec-2] .w.s.m.m.a.ServletInvocableHandlerMethod : Could not resolve parameter [0] in public java.lang.String com.fiserv.report.generator.controller.ReportController.homeSubmit(com.fiserv.report.generator.model.HomeFormInput,org.springframework.ui.Model,org.springframework.validation.BindingResult): org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'homeFormInput' on field 'input': rejected value []; codes [NotEmpty.homeFormInput.input,NotEmpty.input,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [homeFormInput.input,input]; arguments []; default message [input]]; default message [You must provide at least one test plan id] 2021-01-17 20:32:51.791 WARN 20412 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'homeFormInput' on field 'input': rejected value []; codes [NotEmpty.homeFormInput.input,NotEmpty.input,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [homeFormInput.input,input]; arguments []; default message [input]]; default message [You must provide at least one test plan id]] 2021-01-17 20:32:51.791 DEBUG 20412 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed 400 BAD_REQUEST 2021-01-17 20:32:51.792 DEBUG 20412 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost] : Processing ErrorPage[errorCode=0, location=/error] 2021-01-17 20:32:51.796 DEBUG 20412 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for POST "/error", parameters={masked}

How to make it enter the method to handle the validation error?

jazzyekim
  • 101
  • 1
  • 10

1 Answers1

0

It turned out that the change in the list of parameters of the controller does the trick - I removed Model from the parameter's list and it works as charm now. So the final signature of the controller looks like this:

@PostMapping("/report")
public String reportSubmit(@Valid @ModelAttribute HomeFormInput homeFormInput, BindingResult bindingResult) {
...
}

Here is the important part from the documentation

Errors, BindingResult For access to errors from validation and data binding for a command object (that is, a @ModelAttribute argument) or errors from the validation of a @RequestBody or @RequestPart arguments. You must declare an Errors, or BindingResult argument immediately after the validated method argument.

jazzyekim
  • 101
  • 1
  • 10