Skip to content

@Pattern error message "{0}" is not resolved. #14163

Closed
@yoshikawaa

Description

@yoshikawaa

Application

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: external-projectFor an external project and not something we can fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions