Note that there are some explanatory texts on larger screens.

plurals
  1. POJSR 303 custom constraint message
    primarykey
    data
    text
    <p>On a Spring MVC application I've developed a custom JSR 303 constraint as follows:</p> <pre><code>@Target({ElementType.FIELD, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = {}) @Pattern(regexp = EmailWithTld.PATTERN, flags = Pattern.Flag.CASE_INSENSITIVE) public @interface EmailWithTld { static final String PATTERN = "(?:[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"; String message() default "{springtest.constraints.EmailWithTld.message}"; Class&lt;?&gt;[] groups() default {}; Class&lt;? extends Payload&gt;[] payload() default {}; } </code></pre> <p>The problem is that the message value is ignored, and the message that is displayed on validation error is the one from @Pattern ("must match "(?:[a-z0-9!#$%&amp;.....")</p> <p>I was expecting it to return my message, from the message variable.</p> <p>One way i can get a custom message is by replacing the above @Pattern with</p> <pre><code>@Pattern(message="{springtest.constraints.EmailWithTld.message}", regexp = EmailWithTld.PATTERN, flags = Pattern.Flag.CASE_INSENSITIVE) </code></pre> <p>If I do it this way the correct message does show up, provided I use the resource bundle for setting it. If set via the annotation usage, it does not work. Example:</p> <pre><code>@Pattern(message="Testing 1 2 3}" </code></pre> <p>What is the correct way to do it?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload