Custom Variables in JSF Converter's Error Message - validation

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.

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

Related

individual error message for each jsf validator tag

I have an input field which must meet the following restrictions: the input data should have exactly 2 characters, it should accept only letters and numbers, and it must be uppercase. So far, i did the following:
<h:inputText id="code" >
<f:validateLength minimum="2" maximum="2" />
<f:validateRegex pattern="^[a-zA-Z0-9]*$"/>
<f:validateRegex pattern="^[A-Z0-9]*$"/>
</h:inputText>
However, i need individual error messages when any of these validations fails. I did some search, and i found the following UNsuitable solutions:
1) How to customize JSF validation error message : Providing an validatorMessage attribute inside the text tag won't do the job, because this way i can provide only one message per input text tag.
2) http://incepttechnologies.blogspot.ro/p/validation-in-jsf.html : Validation method in the backing bean, or imperative validation using #FacesValidator annotation, are not good because this is exactly what i try to avoid; i want to move validation from back-end to front-end
3) https://www.mkyong.com/jsf2/customize-validation-error-message-in-jsf-2-0/ : overriding the error message in messages.properties is not good for two reasons: 1. I want to use custom error messages only locally (page scope), not for the entire application. 2. I have the same validator tag occuring twice - but with different patterns, and i want a different error message for each situation
The documentation for the validation tags (http://docs.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/ and http://docs.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/f/validateLength.html) specifies no attribute such as 'message', or 'errorMessage', as I hoped.
I have not tFied Kt but I think a woraround will be to place the custom overridden validation messages in a locale specific (which can be used only for this case ) properties file and use locale attribute of f:view tag to set the locale at the page level so that the messages are displayed from the locale specific message bundle.
A temporary solution is to concatenate all error messages into one big message. In case any validation fails, the corresponding message is anyway included in the big message.
So i made use of the validatorMessage attribute in the inputText tag.
Also, in order not to get the large message multiple times (when more than one of the validations fails) i replaced the 3 validations with an equivalent one:
<f:validateRegex pattern="^[A-Z0-9]{2}$"/>

Validation message of an inputFile is thrown in the console [duplicate]

I created an user interface with a form to get information from user. When I submit the form, it prints the following warning in the server log:
INFO: WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=j_idt7:j_idt11[severity=(ERROR 2), summary=(j_idt7:j_idt11: Validation Error: Value is not valid), detail=(j_idt7:j_idt11: Validation Error: Value is not valid)]
I tried to solve it, however I didn't understand how. How is this problem caused and how can I solve it?
As to the warning referring an undisplayed Validation Error: Value is not valid message, this means that you've somewhere a <h:selectXxx> component such as <h:selectOneMenu> without an associated <h:message> and therefore JSF is not able to display faces messages about conversion/validation errors directly in the UI.
In order to fix the particular warning, just add a <h:message>:
<h:selectOneMenu id="foo" ... />
<h:message for="foo" />
Note that when you're ajax-submitting the form using <f:ajax>, then you should not forgot to include the messages in the ajax-update. To start with, use render="#form" to update the entire form.
As to the concrete validation error problem which is mentioned in the message, head to the following answer: Validation Error: Value is not valid.
If you are using primefaces, dont forget to add:
<h:form>
<p:growl id="messages" showDetail="true" />
....
</h:form>
It is invalid value problem. Most probably you entered character instead of integer value. Check this and I suggest exception handling to not face these type of problems again.

OmniFaces validateOrder disabling

I'm trying to use validateOrder component to validate two java.util.Date objects. It is similar to showcase example on this link (PrimeFaces example). Everything works perfect, but i have one question:
What if 2nd date field is not required?
In that case i'm getting nullpointer exception, and since validateOrder has "disabled" attribute, i was wondering is it worth/possible enabling/disabling it via ajax every time the 2nd date is inserted/removed. If not, i guess i'll stick to Balus' approach for JSF2.0 cross-field validation that you can read about on this link.
Let the disabled attribute check if the 2nd field is filled in. If it's not filled in, the request parameter value associated with field's client ID will be empty. Use exaclty that to let disabled attribute evaluate to true.
<p:calendar ... binding="#{endDate}" />
...
<o:validateOrder ... disabled="#{empty param[endDate.clientId]}" />
Code is as-is. No additional backing bean property necessary for binding.
See also:
How does the 'binding' attribute work in JSF? When and how should it be used?

validatorMessage not used when non-number input is entered

I'd like to show a custom message when the user enters a non-number.
For that, I specified validatorMessage attribute:
<h:inputText value="#{bean.commission}" required="true"
requiredMessage="Please enter a value"
validatorMessage="Please enter a number" />
The required message appears correctly on empty input, but the validator message doesn't appear on non-number input. Instead, it shows the default message that the input value is not a number.
How is this caused and how can I solve it?
You faced a conversion error, not a validation error.
Use converterMessage instead.
<h:inputText value="#{bean.commission}" required="true"
requiredMessage="Please enter a value"
converterMessage="Please enter a number" />
The validatorMessage is only used on validators specified via validator attribute and those via nested <f:validator> and <f:validateXxx> tags.
The converterMessage is used on converters specified via converter attribute and those via nested <f:converter> and <f:convertXxx> tags, and also on auto-registered converters for a specific class, such as IntegerConverter in case of int/Integer typed value.
See also:
Accept only digits for h:inputText value
How validate number fields with validateRegex in a JSF-Page?

JSF: remove client id (label, component id) from validation message without a custom message

I have the following code:
<h:inputText id="it-date" value="#{myBean.myDate}">
<f:convertDateTime pattern="MM/dd/yyyy"/>
</h:inputText>
<h:message for="it-date" />
When I enter the date 02/30/2012, I get the following message:
j_idt5:it-date: '02/30/2012' could not be understood as a date. Example: 10/01/2012
Is it possible to remove j_idt5:it-date: including the colon and to keep the default message from Java?
I already tried to use the attribute label, but with that the colon remained. I don't want to use a custom message, because the default messages are already internationalized.
I guess you will need to get in between the message handling somehow to remove the colon because it is a fixed part of the message.
Checkout the message properties file, e.g. here:
http://grepcode.com/file/repo1.maven.org/maven2/com.sun.faces/jsf-api/2.1.7/javax/faces/Messages.properties
The specific message/property in this case would be:
javax.faces.converter.DateTimeConverter.DATE={2}: ''{0}'' could not be understood as a date.
Check out the fixed colon after the {2} . If you want to get rid of it but keep the actual message you would need some way to get in between the message handling.
Mabye this answer from BalusC is an option:
FacesMessage listener

Resources