spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.55k stars 38.12k forks source link

@DateTimeFormat annotations are necessary both on field and constructor parameter in the immutable form class [SPR-16840] #21380

Closed spring-projects-issues closed 6 years ago

spring-projects-issues commented 6 years ago

Masatoshi Tada opened SPR-16840 and commented

public class SampleForm {

@NotBlank
private String message;

@NotNull
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate birthday;

public SampleForm(String message, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate birthday) {
    this.message = message;
    this.birthday = birthday;
}

public String getMessage() {
    return message;
}

public LocalDate getBirthday() {
    return birthday;
}

}

If field's annotation is missing, a value in textbox become "yy/MM/dd" when returning to input view. If constructor parameter's annotation is missing, a validation error occurs in spite of inputting correct value. So I have to add annotations to both of field and constructor parameter. I think this is verbose.


Affects: 5.0.6

Reference URL: https://github.com/MasatoshiTada/spring-mvc-form-sample/blob/master/01-immutable/src/main/java/com/example/immutable/SampleForm.java

Issue Links:

Referenced from: commits https://github.com/spring-projects/spring-framework/commit/955665b419ec237029abfa0824ca8a47d410a423

spring-projects-issues commented 6 years ago

Juergen Hoeller commented

This will be available in the upcoming 5.1.0.BUILD-SNAPSHOT. Please give it an early try...

spring-projects-issues commented 6 years ago

Masatoshi Tada commented

I upgraded my sample to Spring 5.1.RC1, and I found this issue was fixed! Thanks!

https://github.com/MasatoshiTada/spring-mvc-form-sample/commit/03357f6c9c6483363eda323cebe0462b4d5ed622#diff-e8fbdaff32189592f4b1858a9f878268