Search code examples
javavalidationjsf-2resourcebundle

Group separator in jsf validation messages


I have the following validator for the year of birth:

<f:validateLongRange minimum="1920" maximum="1992" />

The validation message is some kind of:

"The year of birth must be between 1.920 and 1.992"

Now some of the users complain: "Do I have to put a dot between the 1 and the 9?"

The answer is of course "No", but can I somehow change the error message in a way that it doesn't show the separator?

(I have changed the default validation message in a custom message bundle using the placeholders {0} and {1} to insert the min and max value, but I think that is not important here, isn't it?)


Solution

  • This number format is been set by LongRangeValidator which in turn uses NumberConverter to prepare the min/max values for the placeholders. So, you can unfortunately not use the SubformatPattern feature of the MessageFormat API.

    Your best bet is to use validatorMessage attribute instead

    <h:inputText validatorMessage="The year of birth must be between 1920 and 1992">
    

    or to supply a custom validator which extends LongRangeValidator and does the message handling job differently (i.e. don't pass the converted min/max value as String to it, but just pass it unconverted). You should then be able to use the following message format:

    The {2} must be between {0,number,#} and {1,number,#}