Skip to content

@Pattern error message "{0}" is not resolved. [SPR-17217] #21750

Open
@spring-projects-issues

Description

@spring-projects-issues

yoshikawaa opened SPR-17217 and commented

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}.


Affects: 5.0.8

Reference URL: spring-projects/spring-boot#14163

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions