I am trying to have constraint validation on the field, which must reject if the input is not an integer.
public class ClientTO {
private Integer phone_num; //
}
I tried:
1) @Digits
- It does not validate if input is integer or not as you could see the type mismatch exception is still thrown.
2) My custom validation - which seems to not work on Integer
fields
Error:
Exception in thread "main" javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.Integer.
My custom validation class:
public class X_CVAImpl implements ConstraintValidator<X_CustomValidatorAnnotation,String>{
public boolean isValid(String value, ConstraintValidatorContext context) {
// TODO Auto-generated method stub
boolean val_d;
if (value.matches("[0-9]+") && value.length() ==10) {
val_d=true;
}else {
val_d=false;
}
return val_d;
}
}
Any help please?
String
fields, but you annotated anInteger
field with it. Beside that, why didn't you just apply the@Pattern
constraint on a string field? – SuricateClientTO
to put it into MVC model, I did not want use string field as I wanted the entity properties of proper type so there wouldn't be any issues when put it into MVC model. trying to work on validator forInteger
as you suggested. – BurnsInteger
, then the only constraint you need is perhaps@NotNull
, and whatever you use for serialization will take care of making sure that the data type is valid. – Suricate