Play constraint messages with arguments

209 views Asked by At

I'm trying to make a list of generic externalized messages in the play framework but can't find or figure out the syntax to pass parameters to these messages within objects that I'm creating. I can successfully used the error messages I've created without the arguments.

I've tried several different ways of declaring the arguments but nothing works Example:

conf/messages: error.number.fixed={0} must be {1} digits

public class Customer {
@Required(message = "error.number.fixed('Phone', '10')" )
public String phone;
}

Output: {"id":["error.number.fixed('Phone', '10'"]}

Without the arguments, the following works:

public class Customer {
@Required(message = "error.number.fixed" )
public String phone;
}

Output: {"id":["[] must be {1} digits"]}

1

There are 1 answers

0
user3297890 On

Figured this one out.

Had to create a phone number annotation with phoneType as property.

public @interface PhoneNumber { String message() default "error.phoneNumber";

...

String phoneType();

...

}

Then set the first argument ({0}) in the messages: error.phoneNumber={0} must be 10 digits.

Then when using the annotation set phonetype as the first (in this case) the only argument for the annotation.

@PhoneNumber(phoneType="MDN")

Please correct me if anyone sees a better way of handling this.