0

I am passing data to two objects from a single html form and in controller I have to use two objects. So, I want to know if I can use the BindingResult for each object in same controller in order to show the errors if it occurred.

SURAJ KUMAR
  • 57
  • 1
  • 11

1 Answers1

2

If you receive two objects from one form in the controller, you must have two BindingResults beacause one BindingResult contains the result of the validation of the object right before it in the signature of the controller's method.

So your controller should look like that :

@PostMapping("...")
public String handlePost(@Valid Object1 object1, BindingResult result1, @Valid Object2 object2, BindingResult result2) {

Maybe you should have look at this post

Romain Warnan
  • 1,022
  • 1
  • 7
  • 13