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?
Related
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}$"/>
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.
I am using the OmniFaces <o:viewParam> taghandler in my current project. I like it, it's great. And even greater is the <o:viewParamValidationFailed>. Now, we are able to send an error if validation or conversion fails. But I wonder, whether it is possible to distinguish between conversion failure and validation failure.
Let's say we want to send a Bad Request if the given view param in malformed and can not be converted; for that matter send a Not Found if conversion succeeded, but the object could not be found in the database; and send a Forbidden if the successfully fetched object should not be accessed by the user.
Does anybody know a way to achieve this?
It's unfortunately not possible to distinguish between a ConverterException and ValidatorException when you've only UIInput#isValid() at hands. Theoretically, you could check and test the faces message to see if it represents a conversion or validation error, but this is not a robust approach, certainly not when it's localized.
On the other hand, it's possible to declare multiple view parameters on the same parameter name. You do not necessarily need to specify a value to set it as model value.
Here's an example based on your description, note that the model value is only set on the last one:
<o:viewParam name="foo">
<f:converter converterId="yourFooConverter" />
<o:viewParamValidationFailed sendError="400" />
</o:viewParam>
<o:viewParam name="foo">
<f:converter converterId="yourFooConverter" />
<f:validateRequired />
<o:viewParamValidationFailed sendError="404" />
</o:viewParam>
<o:viewParam name="foo" value="#{bean.foo}">
<f:converter converterId="yourFooConverter" />
<f:validateRequired />
<f:validator validatorId="yourRestrictedAccessValidator" />
<o:viewParamValidationFailed sendError="403" />
</o:viewParam>
To avoid the expensive job of calling the DB on every conversion, let the YourFooConverter implementation store the converted value as a custom attribute of the FacesContext and then check it on every pass.
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
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