I want to create validator for input filed in order to check values and send error message if the inserted value is not int.
bean:
public class PricingCalculatorValidator implements Validator
{
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
{
// Cast the value of the entered input to String.
String input = (String) value;
// Check if they both are filled in.
if (input == null || input.isEmpty())
{
return; // Let required="true" do its job.
}
// Compare the input with the confirm input.
if (containsDigit(input))
{
throw new ValidatorException(new FacesMessage("Value is not number."));
}
}
public final boolean containsDigit(String s)
{
boolean containsDigit = false;
if (s != null && !s.isEmpty())
{
for (char c : s.toCharArray())
{
if (containsDigit = Character.isDigit(c))
{
break;
}
}
}
return containsDigit;
}
}
What is the proper way to cast the inserted value? Now I get exception
serverError: class java.lang.ClassCastException java.lang.Integer cannot be cast to java.lang.String
As per the exception, JSF is actually passing an Integer
instance as value
to the validate()
method. Technically, you should be casting it to Integer
as below to keep the Java runtime happy.
// Cast the value of the entered input to Integer.
Integer input = (Integer) value;
Apparently you already bound the input field to an Integer
property in the model like so:
<h:inputText value="#{bean.pricing}" />
private Integer pricing;
In other words, the whole custom validator is unnecessary. There's no point of validating if the value is an integer if it's already an Integer
in first place. It can impossibly contain a non-digit as value.
Just get rid of that custom validator altogether.
JSF has several builtin converters for standard types like Integer
which run automatically depending on the model value type. And, converters run right before validators. That's why the value already arrives as Integer
in your custom validator. The only thing which seems relevant in your custom validator is the error message which is different from the standard conversion error message. In case you merely wanted to customize the conversion error message on the specific input field, just set it as converterMessage
attribute on the input field.
<h:inputText value="#{bean.pricing}" converterMessage="Value is not numeric." />
Unrelated to the concrete problem, "validating" if the value represents a valid Integer
or not (this process is in JSF actually called conversion and is supposed to take place in a custom converter) can be performed in a much simpler way:
String string = getItSomehowWithoutClassCastException();
try {
Integer integer = Integer.valueOf(string);
} catch (NumberFormatException e) {
throw new ConverterException(new FacesMessage("Value is not an integer."));
}
No need to inspect every single digit if you ultimately want to get an Integer
. As said, the JSF builtin converter already takes care of this.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments