Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate number string as digit with hibernate?

Tags:

Which annotations would I have to use for Hibernate Validation to validate a String to apply to the following:

//should always have trimmed length = 6, only digits, only positive number @NotEmpty @Size(min = 6, max = 6) public String getNumber {     return number.trim(); } 

How can I apply digit validation? Would I just use @Digits(fraction = 0, integer = 6) here?

like image 538
membersound Avatar asked Oct 23 '13 09:10

membersound


People also ask

Is @valid and @validated the same?

The @Valid annotation ensures the validation of the whole object. Importantly, it performs the validation of the whole object graph. However, this creates issues for scenarios needing only partial validation. On the other hand, we can use @Validated for group validation, including the above partial validation.

Is Hibernate Validator thread safe?

Validating constraints In the setUp() method, a Validator instance is retrieved from the ValidatorFactory . Validator instances are thread-safe and may be reused multiple times.

What does @validated do?

The @Validated annotation is a class-level annotation that we can use to tell Spring to validate parameters that are passed into a method of the annotated class.


2 Answers

You could replace all your constraints with a single @Pattern(regexp="[\\d]{6}"). This would imply a string of length six where each character is a digit.

like image 194
Hardy Avatar answered Oct 02 '22 01:10

Hardy


You can also create your own hibernate validation annotation.
In the example below I created a validation annotation with name EnsureNumber. Fields with this annotation will validate with the isValid method of the EnsureNumberValidator class.

@Constraint(validatedBy = EnsureNumberValidator.class) @Target({ ElementType.FIELD }) @Retention(RetentionPolicy.RUNTIME) public @interface EnsureNumber {      String message() default "{PasswordMatch}";      Class<?>[] groups() default {};      Class<? extends Payload>[] payload() default {};      boolean decimal() default false;  }  public class EnsureNumberValidator implements ConstraintValidator<EnsureNumber, Object> {     private EnsureNumber ensureNumber;      @Override     public void initialize(EnsureNumber constraintAnnotation) {         this.ensureNumber = constraintAnnotation;     }      @Override     public boolean isValid(Object value, ConstraintValidatorContext context) {         // Check the state of the Adminstrator.         if (value == null) {             return false;         }          // Initialize it.         String regex = ensureNumber.decimal() ? "-?[0-9][0-9\\.\\,]*" : "-?[0-9]+";         String data = String.valueOf(value);         return data.matches(regex);     }  } 

You can use it like this,

@NotEmpty @Size(min = 6, max = 6) @EnsureNumber private String number1;  @NotEmpty @Size(min = 6, max = 6) @EnsureNumber(message = "Field number2 is not valid.") private String number2;  @NotEmpty @Size(min = 6, max = 6) @EnsureNumber(decimal = true, message = "Field number is not valid.") private String number3; 
like image 26
Georgios Syngouroglou Avatar answered Oct 02 '22 02:10

Georgios Syngouroglou