Closed
Description
Application
- Spring Boot Web MVC Application
- Spring Boot 2.0.4.RELEASE
- Sample : https://github.com/yoshikawaa/spring-boot-pattern-demo
Problem
There are cases where @Pattern
error message {0}
is not resolved.
- ValidationMessages.properties
javax.validation.constraints.Pattern.message = {0} must match "{regexp}".
- application.properties
spring.messages.basename = ValidationMessages
- Form
public class DemoForm {
@Pattern(regexp = "\\d{3}") // -> message [ valid must match "\d{3}". ]
private String valid;
@Pattern(regexp = "\\d{1,3}") // -> message [ {0} must match "\d{1,3}". ]
private String invalid;
// omit getter and setter
}
My validation is that if a range of digits is used in a regular expression, the mechanism for resolving {0}
does not work properly.
- Controller
@PostMapping
public String post(@Valid DemoForm form, BindingResult result) {
result.getFieldErrors().forEach(e -> logger.info("field:{},error:{}", e.getField(), e.getDefaultMessage()));
// log [ field:valid,error:{0} must match "\d{3}". ]
// log [ field:invalid,error:{0} must match "\d{1,3}". ]
return "demo";
}
When logging BindingResult with Controller, you can see that LocalValidatorFactoryBean
(Hibernate Validator) resolves message variable {regexp}
beforehand.
After that, when Spring MVC resolve the message, it is considered that the regular expression in the message is obstructing resolution of {0}
.