I’m making a simple form with an input field implementing Bean Validation. All of the validations work fine. The issue is the message rendered (or lack of rendering) when the user enters a non numeric value:
-
If I use the @Pattern check in the Bean, no warning message is displayed in the view when the user enters a non numeric value.
-
If I remove the @Pattern from the Bean, the following warning message shows up when the user enters a non numberic value:
“j_idt8:age: ‘some string’ debe ser un número entre -2147483648 y 2147483647. Ejemplo: 9346”
which is translated to: “j_idt8:age: ‘some string’ must be a number between -2147483648 and 2147483647. Example: 9346”.
Is there anything I’m missing? Is there any way to implement a simple message warning? or is there anyway to customize the default message of: “j_idt8:age: ‘some string’ debe ser un número entre -2147483648 y 2147483647. Ejemplo: 9346”?
These are some code excerpt, in case you ask for them:
web.xml
<context-param>
<description>JSF will interpret empty strings submitted values as null</description>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
user Bean:
@NotNull(message = "Please enter your age")
@Pattern(regexp = "[0-9][0-9]", message = "Age must be numeric")
@Max(99)
@Min(18)
private Integer age;
Facelets:
<h:form>
Enter your age:<br/>
<h:inputText id="age" value="#{user.age}">
<f:ajax event="blur" render="ageMessage" />
</h:inputText>
<h:message id="ageMessage" for="age" /><br/>
<h:commandButton value="Insert" action="#{user.showChoice}">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
</h:form>
You’re facing a conversion error, not a validation error. The problem manifests long before the validation can ever take place. It’s the builtin JSF
String–Integerconversion which failed. Note that the@Patternmakes no sense on anIntegerproperty. You cannot set it with an arbitraryStringvalue.If you change the property to be a
String, then you’ll see the “expected” results. But keeping itIntegeris of course better if it should really represent a number. You can finetune the JSF converter message by theconverterMessageattribute, or by supplying a customConverterimplementation, or by overriding the default message bundle file in<message-bundle>.