Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Variables in JSF Converter's Error Message

I have a form page that has an inputText field that accepts a date. We have a converter that converts the string from the textbox into a Date object (ie. "2011-03-01" to java.util.Date("2011-03-01"") )

If the string is not a date, like "123" then a validation error message will be displayed like "value (123) must be a date".

Currently, in my .properties file, I see:

javax.faces.converter.DateTimeConverter.DATE=value ({0}) must be a date

I need to make this error message more clear by specifying exactly which field must be a date. (As there could be more than one date text fields on the form).

I would like to change it to something like:

javax.faces.converter.DateTimeConverter.DATE=The field "{0}" with value ({1}) must be a date

However, I am unsure how JSF automatically fills in the {0} and {1}. How do I specify my own variables inside the JSF Converter error message?

Note: I have added tried creating my own validator (not to be confused with converter) but it seems that the JSF framework does conversion before validation in its lifecycle.

like image 407
Steve Avatar asked Mar 04 '11 22:03

Steve


1 Answers

Starting from JSF 1.2, use the converterMessage attribute to replace the entire message, such as:

<h:inputText value="#{user.dateOfBirth}" converterMessage="Format must be: yyyy-MM-dd">
    <f:convertDateTime pattern="yyyy-MM-dd" />
</h:inputText>

Otherwise, JSF by default shows the _detail message in the <h:message>. Only when you use <h:message showDetail="false" showSummary="true"> then the one similar as in your question will be displayed. I'm not sure what JSF version you're using, but in my JSF 2.0.3 the default detail message for f:convertDateTime is the following:

javax.faces.converter.DateTimeConverter.DATE_detail = {2}: ''{0}'' could not be understood as a date. Example: {1}

The {2} will be substituted with the client ID or the label attribute of the input field when present.

<h:inputText value="#{user.dateOfBirth}" label="Date of birth">
    <f:convertDateTime pattern="yyyy-MM-dd" />
</h:inputText>

Both the DATE and DATE_detail message must be defined for the DATE_detail message to be used:

javax.faces.converter.DateTimeConverter.DATE=Date format must be: dd/mm/yyyy 
javax.faces.converter.DateTimeConverter.DATE_detail=Date format must be: dd/mm/yyyy

See also:

  • JSF 2.0 tutorial - Finetuning validation
like image 111
BalusC Avatar answered Oct 04 '22 01:10

BalusC