Bean Validation's resourcebundle parameterization possibility in JSF 2?

Maybe you already know that messages for Bean Validation are defined in the Resource Bundle ValidationMessage. Properties in the root of your classes (i. E WEB-INF\classes\ValidationMessages.

Properties).

Maybe you already know that messages for Bean Validation are defined in the Resource Bundle ValidationMessage. Properties in the root of your classes (i. E WEB-INF\classes\ValidationMessages.

Properties). These messages can have parameters but they don't work as in JSF. There is a interface called MessageInterpolator that transform the message pattern into the actual message.

Default interpolator works with named parameters like in the message: Value must be between {min} and {max}. The values between { and } are resolved first in resource bundle of the application; later in the resource bundle of the provider, and last in properties of the constraint annotation. (This is more or less how it works, the complete algorithm is in section 4.3 of the Bean Validation specification).

Suppose you define the attribute message of the Size annotation as {creditCard. Message} The content of ValidationMessage. Properties could be creditCard.

Message=Credit card length must be at least {min} characters. \ Thank you for choosing the plantsvszombies credit card. You could replace plantsvszombies with a property: creditCard.

Message=Credit card length must be at least {min} characters. \ Thank you for choosing the {creditCard. Type} credit card.CreditCard.

Type=plantsvszombies You could even use two parameters in the message of the constraint Size(min=13, message="{creditCard. Message} {plantsvszombies. Messages}") and define the resource bundle as creditCard.

Message=Credit card length must be at least {min} characters. Plantsvszombies. Message=Thank you for choosing the plantsvszombies credit card.

I think this is a simple and clean approach. But if you want something more like defining custom parameters in the declaration of the constraint you could use a custom message interpolator. Please notice that this could be a trickier solution.

Well, you could define a syntax to introduce your parameters in the message string. Then, let the default interpolator resolve the message. The syntax for custom parameters won't be understood by the default interpolator and they will still be there after resolving.

Then, the custom interpolator can replace replace the custom parameters. This is more easy to understand with an example. First, a message is defined like {creditCard.

Message}plantsvszombies. For this syntax, the content between the square brackets are the indexed parameters separated by commas (here is only one parameter). Next, the content of the resource bundle is defined with: creditCard.

Message=Credit card length must be at least {min} characters. \ Thank you for choosing the {0} credit card. When the default interpolator replaces the first part of the expression, we'll have: Credit card length must be at least 13 characters.

\ Thank you for choosing the {0} credit card. Plantsvszombies Then, the custom interpolator will take the last expression and split the content to get the tokens and replace the indexed parameters with the token in the corresponding index (parameter0=plantsvzzombies). So the message will be: Credit card length must be at least 13 characters.

\ Thank you for choosing the plantsvszombies credit card. This is the code of the custom interpolator for this syntax (not optimized and the regex pattern could not work if there are other square brackets in the first expression or in the tokens). Package validation; import java.util.

Locale; import java.util.regex. Matcher; import java.util.regex. Pattern; import javax.validation.

MessageInterpolator; import javax.validation. Validation; public class MyInterpolator implements MessageInterpolator{ private MessageInterpolator interpolator; public MyInterpolator() { //we need to delegate to the default interpolator this. Interpolator = Validation.

ByDefaultProvider().configure(). GetDefaultMessageInterpolator(); } private static final Pattern parametersPattern=Pattern. Compile("\\(.+)\\$"); protected static String replaceParameters(String message){ Matcher matcher = parametersPattern.

Matcher(message); String values={}; if(matcher.find()){ values=matcher. Group(1). Split("\\s*,\\s*"); message=message.

Substring(0, matcher.start()); for(int i=0; I validation. MyInterpolator This is a bit complicated solution because the constraint annotations doesn't accept parameters for the messages and because in the interpolator, we can not get many information of the property that is being validated. If I find an easier solution, I will post it.

Wow, such detailed explanation. Thank you for your effort and sharing! Would like to try out these solutions soon!

– Albert Kam Mar 29 at 3:13 You're welcome. :) – victor herrera Mar 29 at 5:52.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions