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