Skip to content

Failure to bind a component of a form backing object using constructor binding causes the object to be null #24372

@odrotbohm

Description

@odrotbohm

Assuming a form backing object:

class Form {

  private final LocalDate date;
  private final LocalTime time;

  public Form(LocalDate date, LocalTime time) {
    this.date = date;
    this.time = time;
  }

  // Getters omitted
}

and a controller method

class FormController {

  @PostMapping("/form")
  String postForm(@ModelAttribute Form form, BindingResult result) {
    …
  }
}

If the there's a binding error for one of the components of Form, form will be null which is unexpected as the binding invalidly assumes that Form is not able to deal with null values for its components.

In contrast to binding via setters, it's not really possible to use the partially bound instance to redisplay the form even partially bound. I've also tried to explicitly annotate the constructor arguments with @Nullable but that doesn't seem to change anything.

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions