The situation . I would like to perform Hibernate Validation based on user properties (to allow different validation rules for input based on user account information). I think it should be possible to use a .properties file to specify a specific regular expression, but I cannot figure out what is wrong:
My current method of specifying a regular expression checks this regular expression from a constant in a specific interface file (so that everything is together) and inserts it as a constant into the annotation @Pattern()for each variable - for example, for a variable workPhone:
@Column(name = "WORK_PHONE")
@NotEmpty(message = "{ContactInfo.workPhone.notEmpty}")
@Pattern(regexp = PHONE_NUMBER_PATTERN_SL, message = "{ContactInfo.workPhone.regexp.msg}")
@Size(max = 10, message = "{ContactInfo.workPhone.size}")
protected String workPhone;
... where the regular expression is stored in static final String PHONE_NUMBER_PATTERN_SL, and all calls {ContactInfo.workPhone...}come from the .properties file:
ContactInfo.workPhone.notEmpty=Please enter your phone number.
ContactInfo.workPhone.regexp.msg=Invalid characters entered in phone. Use this format XXX-XXX-XXXX.
ContactInfo.workPhone.size=Phone can not be longer than 10 digits.
Unfortunately, this layout makes the validation template system-wide (compiled), because I cannot figure out how to change it for another user in another company, location, employment, etc. To make it possible to differentiate based on this information, I would also like to save the regular expression in the properties file, and I am trying to include it this way:
ContactInfo.workPhone.regexp=\d{3}-\d{3}-\d{4}
including the link in the annotations from the third line in the first code list:
@Pattern(regexp = "{ContactInfo.workPhone.regexp}", message = "{ContactInfo.workPhone.regexp.msg}")
Then I turned off the property files for different cases, for example, to allow / require a phone number format other than US
: , ? ( )?
, ( ), , - @Pattern Hibernate, , .
TL; DR. , Hibernate Pattern Validation, ?